"ZJOI2006" logistics and transport

topic

[Memory Limit: $ 256MiB $] [Time limit: $ 1000ms $]
O [Standard] [Title Type: Traditional] [Evaluation method: Text Comparison]

Description [title]

Logistics company put a number of goods from the pier to pier A B. As the goods than larger, it requires $ n $ days to complete operation. Transportation of goods in general to turn several parked pier. Logistics companies often design a fixed transport routes for the implementation of strict management and tracking of the entire transport process.
Due to various factors, sometimes a pier will not load and unload cargo. This time we have to change transport routes, so that goods can reach their destinations on time. But revised route is a very troublesome thing, will bring additional costs, logistics companies want to set a $ n $-day transportation plan, so that the total cost as low as possible.

[Input Format]

The first line four integer $ n (1 \ le n \ le 100), m (1 \ le m \ le 20), K, e $. $ N $ denote the number of days required for the transport of goods, $ m $ is the total number wharf, $ K $ denote each modification costs required to transport routes.

Next, $ E $ lines each one route is described, comprising the three integers, respectively for these two terminals connected in number and length of these $ (> 0) $. Wherein A Harbor numbered $ 1 $, Terminal B number $ m $. Transportation costs per unit length is 1. Route in both directions.

The next line is an integer $ d $, $ D $ row behind each row of three integers $ P (1 <P <m), a, b (1 \ le a \ le b \ le n) $. That number is $ P $ A $ from the pier to day $ $ b $ days not loading and unloading (including head and tail). With a marina, possibly in multiple time periods is not available. But any time there is at least one pier from $ A $ to $ B $ dock transport routes.

[Output format]

It includes an integer representing the minimum total cost. Total cost $ = n $ days and the length of the transport route $ + K \ times $ the number of transportation routes change.

[Sample]

Sample input

5 5 10 8
1 2 1
1 3 3
1 4 2
2 3 2
2 4 4
3 4 1
3 5 2
4 5 2
4
2 2 3
3 1 1
3 3 3
4 4 5

Sample Output

32

Sample [explain]


The figure represents the order of $ 1 to $ $ $ 5 days, the hatching unavailable dock.

The optimal solution is: the first three days away $ 1 \ rightarrow 4 \ rightarrow 5 $, two days to go $ 1 \ rightarrow 3 \ rightarrow 5 $, so the total cost was $ (2 + 2) \ times 3+ (3 + 2) \ times 2 + 10 = 32 $.

answer

[Managers] do title

First of all, this question involves a short circuit is most definitely see that.

But then, in the end how to deal with the situation can not be some way to go yet?

This time, I think the most short-circuit and short-circuits, it is processed in a similar way to the tree $ dp $.

But so want to have a $ bug $, that is the situation in case of the most short-circuit and short-circuits are dead end it?

Then I started crazy fantasy times and short-circuit, short-circuit times and times ......

[Correct]

I'm considering this question, when this question ignores the data range: $ n (1 \ le n \ le 100), m (1 \ le m \ le 20) $

You can see $ n $ and $ m $ is very small, so why do not we do it direct violence.

But this violence is also based on the basis of $ dp $ on.

First, you can set up a simple state:

$ Dp [i] $: minimum cost of $ I $ day to be used

Then turn and call out like

$dp[i]=dp[j]+cost[j+1][i]*(i-j)+K$

Which, $ j $ is we change the course of the day. This shaped turn means that from the $ j $ day began to replace the new channel, the first $ j + 1 $ day to the first $ i $ days using the same channel, the first $ j $-day how to go no matter, because this is things in front of treatment does not affect our stage.

So now only a question: $ cost [i] [j] $ how demand it?

Starting from the definition:

$ Cost [i] [j] $: from $ $ I $ J $ to day with the same minimum cost day waterway.

Go back to the previously mentioned, this question is very small data range, then we can deal with the direct violence $ cost [i] [j] $

Then the code will come out

#include<bits/stdc++.h>
using namespace std;
#define int long long
template<class T>inline void qread(T& x){
    char c;bool f=false;x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x; 
}
template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
inline int rqread(){
    char c;bool f=false;int x=0;
    while((c=getchar())<'0'||'9'<c)if(c=='-')f=true;
    for(x=(c^48);'0'<=(c=getchar())&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
const int MAXN=100;
const int MAXM=20;
class gragh{
    #define EDGE_SIZE 400
    #define NODE_SIZE 20
    #define INF 0x3f3f3f3f
public:
    int N,M;
    struct edge{int to,nxt,w;
        edge(){}
        edge(const int T,const int Nx,const int W):to(T),nxt(Nx),w(W){}
    }e[EDGE_SIZE+5];
    int tail[NODE_SIZE+5],ind;
    inline void add_edge(const int u,const int to,const int w){e[++ind]=edge(to,tail[u],w);tail[u]=ind;}
    inline void dijkstra(int dis[],const int s,bool vis[]){
        struct node{int u,w;
            node(){}
            node(const int U,const int W):u(U),w(W){}
            bool operator<(const node a)const{return !(w<a.w);}
        };
        for(int i=1;i<=N;++i)dis[i]=INF;
        priority_queue<node>Q;
        Q.push(node(s,dis[s]=0));
        while(!Q.empty()){
            int u=Q.top().u;Q.pop();
            for(int i=tail[u],v;i;i=e[i].nxt)if(dis[v=e[i].to]>dis[u]+e[i].w&&!vis[v])
                Q.push(node(v,(dis[v]=dis[u]+e[i].w)));
        }
    }
    inline bool spfa(int dis[],const int s,bool vis[]){
        int cnt[NODE_SIZE+5];/*bool vis[EDGE_SIZE+5];*/
        queue<int>Q;
        for(int i=1;i<=N;++i)dis[i]=INF/*,vis[i]=false*/,cnt[i]=0;
        Q.push(s);
        dis[s]=0,cnt[s]=1;
        while(!Q.empty()){
            int now=Q.front();Q.pop();
            vis[now]=false,++cnt[now];
            if(cnt[now]>N)return false;
            for(int i=tail[now],v;i;i=e[i].nxt){
                v=e[i].to;
                if(dis[now]+e[i].w<dis[v]){
                    dis[v]=dis[now]+e[i].w;
                    if(!vis[v])Q.push(v),vis[v]=true;
                }
            }
        }
        return true;
    }
    inline void clr(){memset(tail,ind=0,sizeof tail);}
    gragh(){clr();}
    #undef EDGE_SIZE
    #undef NODE_SIZE
    #undef INF
}G;
int n,K,d,cost[MAXN+5][MAXN+5];
bool inf[MAXM+5][MAXN+5];
inline void init(){
    qread(n,G.N,K,G.M);
    for(int i=1,a,b,c;i<=G.M;++i){
        qread(a,b,c);
        G.add_edge(a,b,c);
        G.add_edge(b,a,c);
    }
    qread(d);
    for(int i=1,P,a,b;i<=d;++i){
        qread(P,a,b);
        for(int j=a;j<=b;++j)inf[P][j]=true;
    }
}
void calcCost(){
    bool vis[MAXM+5];int dis[MAXM+5];
    for(int i=1;i<=n;++i)for(int j=i;j<=n;++j){
        memset(vis,0,sizeof vis);
        for(int k=1;k<=G.N;++k)for(int t=i;t<=j;++t)
            if(inf[k][t]){vis[k]=true;break;}
        G.spfa(dis,1,vis);
        cost[i][j]=dis[G.N];
    }
}
inline void getDp(){
    int dp[MAXN+5];
    for(int i=1;i<=n;++i)dp[i]=0x3f3f3f3f;
    for(int i=1;i<=n;++i){
        dp[i]=cost[1][i]*i;
        for(int j=i-1;j>=0;--j)
            dp[i]=Min(dp[i],dp[j]+cost[j+1][i]*(i-j)+K);
    }
    printf("%lld\n",dp[n]);
}
signed main(){
    init();
    calcCost();
    getDp();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/MachineryCountry/p/11653648.html