[ZJOI2006]物流運輸
阿新 • • 發佈:2017-07-19
ref ons 計劃 不可用 n) ring != () problems
Submit: 8020 Solved: 3353
[Submit][Status][Discuss]
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
//前三天走1-4-5,後兩天走1-3-5,這樣總成本為(2+2)*3+(3+2)*2+10=32 是i到j天共同的最短路的長度,從[j+1,i]天內都使用這條路,相對於j天來講換了一次;可能會想,這個dp在dp[j]會不會跟cost[j+1][i]是相同的,沒關系,因為總會存在一個點使得路徑轉化,j是從0枚舉的,dp取min,所以答案肯定是正確的;
1003: [ZJOI2006]物流運輸
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8020 Solved: 3353
[Submit][Status][Discuss]
Description
物流公司要把一批貨物從碼頭A運到碼頭B。由於貨物量比較大,需要n天才能運完。貨物運輸過程中一般要轉
停好幾個碼頭。物流公司通常會設計一條固定的運輸路線,以便對整個運輸過程實施嚴格的管理和跟蹤。由於各種
因素的存在,有的時候某個碼頭會無法裝卸貨物。這時候就必須修改運輸路線,讓貨物能夠按時到達目的地。但是
修改路線是一件十分麻煩的事情,會帶來額外的成本。因此物流公司希望能夠訂一個n天的運輸計劃,使得總成本
盡可能地小。
Input
第一行是四個整數n(1<=n<=100)、m(1<=m<=20)、K和e。n表示貨物運輸所需天數,m表示碼頭總數,K表示
每次修改運輸路線所需成本。接下來e行每行是一條航線描述,包括了三個整數,依次表示航線連接的兩個碼頭編
號以及航線長度(>0)。其中碼頭A編號為1,碼頭B編號為m。單位長度的運輸費用為1。航線是雙向的。再接下來
一行是一個整數d,後面的d行每行是三個整數P( 1 < P < m)、a、b(1< = a < = b < = n)。表示編號為P的碼
頭從第a天到第b天無法裝卸貨物(含頭尾)。同一個碼頭有可能在多個時間段內不可用。但任何時間都存在至少一
條從碼頭A到碼頭B的運輸路線。
Output
包括了一個整數表示最小的總成本。總成本=n天運輸路線長度之和+K*改變運輸路線的次數。
Sample Input
5 5 10 81 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//前三天走1-4-5,後兩天走1-3-5,這樣總成本為(2+2)*3+(3+2)*2+10=32
一次路線增加K的費用,n天,那麽,可以設狀態dp[i]表示前i天的最小費用,那麽轉移很好想,dp[i]=min(dp[j] + cost[j+1][i]*(i-j) + K),cost[i][j]
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstdio> 5 #include<cmath> 6 #include<map> 7 #include<set> 8 #include<queue> 9 #include<vector> 10 #include<iomanip> 11 #include<cstring> 12 #define inf 1<<29 13 #define ll long long 14 #define db double 15 #define re register 16 #define il inline 17 #define rep(i,a,b) for(register int i=a;i<=b;++i) 18 #define file(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout); 19 using namespace std; 20 const int N=110; 21 struct Edge{ 22 int to,net,w; 23 }e[250*2]; 24 int head[25],num_e,day; 25 int dp[N],cost[N][N],K,n,m; 26 bool can[25][N][N]; 27 il void add(int x,int y,int w) { 28 e[++num_e].to=y,e[num_e].w=w,e[num_e].net=head[x],head[x]=num_e; 29 } 30 inline int gi() { 31 re int res=0,f=1;re char ch=getchar(); 32 while((ch<‘0‘||ch>‘9‘)&&ch!=‘-‘) ch=getchar(); 33 if(ch==‘-‘) f=-1,ch=getchar(); 34 while(ch>=‘0‘&&ch<=‘9‘) res=res*10+ch-‘0‘,ch=getchar(); 35 return res*f; 36 } 37 bool inq[25]; 38 int dis[25]; 39 il void spfa(int a,int b) { 40 for(re int i=1;i<=n;i++) dis[i]=inf; 41 memset(inq,0,sizeof(inq)); 42 queue<int> q; 43 q.push(1); 44 dis[1]=0;inq[1]=1; 45 re int u; 46 while(!q.empty()) { 47 u=q.front();q.pop(); 48 inq[u]=0; 49 for(int i=head[u];i;i=e[i].net) { 50 int to=e[i].to; 51 if(dis[to] > dis[u] + e[i].w && can[to][a][b]) { 52 dis[to]=dis[u] + e[i].w; 53 if(!inq[to]) q.push(to),inq[to]=1; 54 } 55 } 56 } 57 cost[a][b]=dis[n]; 58 } 59 void DP() { 60 rep(i,1,day) dp[i]=inf; 61 dp[0]=-K; 62 for(re int i=1;i<=day;i++) 63 for(re int j=0;j<i;j++) 64 if(dp[j]<inf&&cost[j+1][i]<inf) 65 dp[i]=min(dp[i],dp[j]+cost[j+1][i] * (i-j)+K); 66 } 67 int main(){ 68 file("spfa"); 69 day=gi(),n=gi(),K=gi(),m=gi(); 70 re int u,v,w; 71 while(m--){ 72 u=gi(),v=gi(),w=gi(); 73 add(u,v,w),add(v,u,w); 74 } 75 m=gi(); 76 memset(can,true,sizeof(can)); 77 while(m--){ 78 w=gi(),u=gi(),v=gi(); 79 for(re int i=1;i<=v;i++) 80 for(re int j=u;j<=day;j++) 81 if(i<=j) 82 can[w][i][j]=false; 83 } 84 85 for(re int i=1;i<=day;i++) 86 for(re int j=i;j<=day;j++) 87 spfa(i,j); 88 DP(); 89 printf("%d",dp[day]); 90 return 0; 91 }
[ZJOI2006]物流運輸