Given an undirected graph, transport for N days, some days cannot be taken, the price of changing the route is K, and the sum of the costs is calculated.
First, set cost [I] [J] to the minimum cost of taking the same route from day I to day J. spfa is used for processing.
Then, the issue of motion regulation makes f [I] 1 ~ Minimum I-day spending
Then f [I] = min {f [J] + cost [J + 1] [I] + k} (0 <= j <I)
Note that m and n are not reversed.
Before the number of days, determine whether it is positive or not.
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct abcd{int to,f,next;}table[10000];int head[30],tot;int n,m,_k,e,d,cost[110][110];bool ban[30][110],unusable[30];int q[65540],f[110],v[30];unsigned r,h;void SPFA(){int i;memset(f,0x3f,sizeof f);q[++r]=1;f[1]=0;while(r!=h){int x=q[++h];v[x]=0;for(i=head[x];i;i=table[i].next)if(!unusable[table[i].to])if(f[x]+table[i].f<f[table[i].to]){f[table[i].to]=f[x]+table[i].f;if(!v[table[i].to])v[table[i].to]=1,q[++r]=table[i].to;}}}void Add(int x,int y,int z){table[++tot].to=y;table[tot].f=z;table[tot].next=head[x];head[x]=tot;}int main(){int i,j,k,x,y,z;cin>>n>>m>>_k>>e;for(i=1;i<=e;i++){scanf("%d%d%d",&x,&y,&z);Add(x,y,z);Add(y,x,z);}cin>>d;for(i=1;i<=d;i++){scanf("%d%d%d",&z,&x,&y);for(j=x;j<=y;j++)ban[z][j]=1;}for(i=1;i<=n;i++)for(j=i;j<=n;j++){memset(unusable,0,sizeof unusable);for(x=2;x<m;x++)for(k=i;k<=j;k++)if(ban[x][k]){unusable[x]=1;break;}SPFA();cost[i][j]=f[m]*(f[m]>=0x3f3f3f3f?1:j-i+1);}memset(f,0x3f,sizeof f);f[0]=0;for(i=1;i<=n;i++)for(j=0;j<i;j++)f[i]=min(f[i],f[j]+cost[j+1][i]+_k);cout<<f[n]-_k<<endl;}
Bzoj 1003 zjoi2006 Dynamic Planning of Logistics and Transportation trans + spfa