the
Given an M-without-direction graph, you need to plan N-day transportation solutions. N days, every day from the S walk to T walk once, each point in some days can not pass. The cost is equal to the length of the path each day, and if the path chosen by the first day is different from the I-1 Day (i>1), it will take more than K to change the route price. Ask for the minimum total cost.
(N<=100,M<=20) Solving
Brain hole problem, not too difficult. May at first think of the more distant, if consider the path of change, found difficult to start.
is actually a simple linear DP. Set F[i] represents the total cost of the first day I spent
Transfer equation: f[i]=min{f[j]+w (j+1,i) +k}
where W (i,j) means that the first i~j days are the shortest path of the same road long, the most short-circuit brush.
There may be a doubt: F[i] This state does not contain the information of the path Ah, why not to judge the way W (j+1,i) walk and F[j] is the same. Simply think about it and you'll find that it doesn't affect the answer.
#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
const int maxn=405;
int n,m,w_c,e,f[maxn],dis[maxn],que[10005];
int Fir[maxn],nxt[maxn],son[maxn],w[maxn],tot;
BOOL VIS[MAXN],AC[MAXN][MAXN],NOW[MAXN]; BOOL Add (int x,int y,int z) {son[++tot]=y; w[tot]=z; nxt[tot]=fir[x]; fir[x]=tot;} int SPFA () {if (!) (
NOW[1]&&NOW[M]) return-1;
memset (Vis) (vis,0,sizeof); memset (dis,63,sizeof (dis));
int inf=dis[0];
int head=0,tail=1; dis[1]=0;
Que[1]=1;
while (head!=tail) {int x=que[++head];
Vis[x]=false;
for (int j=fir[x];j;j=nxt[j]) if (Now[son[j]]&&dis[x]+w[j]<dis[son[j]]) {DIS[SON[J]]=DIS[X]+W[J];
if (!vis[son[j]]) vis[son[j]]=true, que[++tail]=son[j];
} return (Dis[m]==inf)? -1:dis[m];
int main () {scanf ("%d%d%d%d", &n,&m,&w_c,&e);
for (int i=1;i<=e;i++) {int x,y,z; scanf ("%d%d%d", &x,&y,&z); AdD (X,Y,Z);
Add (y,x,z);
} memset (Ac,1,sizeof (AC)); int t;
scanf ("%d", &t);
while (t--) {int id,l,r; scanf ("%d%d%d", &id,&l,&r);
for (int i=l;i<=r;i++) Ac[id][i]=false; } memset (F,63,sizeof (f));
F[0]=-w_c;
for (int i=1;i<=n;i++) {memset (now,1,sizeof (now));
for (int j=i;j>=1;j--) {for (int k=1;k<=m;k++) if (!ac[k][j)) Now[k]=false; int RES=SPFA ();
if (res==-1) break;
F[i]=min (f[i],f[j-1]+res* (i-j+1) +w_c);
printf ("%d", f[n]);
return 0; }