Description
The logistics company is going to ship a batch of cargo from Wharf A to Pier B. Due to the large volume of goods, it takes n days to complete the shipment. In the course of cargo transport, several terminals are generally diverted. Logistics companies typically design a fixed transport route to carry out strict management and tracking of the entire transport process. Due to the existence of various factors, sometimes a pier will not be able to load and unload goods. At this time, the transport route must be modified to allow the goods to arrive at their destination. But modifying the route is a very troublesome thing, and will bring additional costs. So logistics companies want to be able to order an N-day shipping plan, making the total cost as small as possible.
Input
The first line is four integers n (1<=n<=100), M (1<=m<=20), K, and E. n indicates the number of days to transport the goods, m represents the total number of docks, and K indicates the cost of each modification of the shipping route. The next line of e lines is a description of the route, including three integers, which in turn represent the two port number of the route connection and the route length (>0). Pier A is numbered 1 and Pier B is M. The transportation cost per unit length is 1. The route is bidirectional. Then the next line is an integer d, followed by a row of D for each row is three integers P (1 < P < m), A, B (1 < = a < = b < = n). Indicates that the pier numbered P is unable to load and unload goods from day A to day B (including tail). The same dock may not be available for multiple time periods. But at any time there is at least one transport route from Pier A to Pier B.
Output
Includes an integer representing the minimum total cost. Total cost =n days the sum of the length of the transportation route +k* Change the number of shipping routes.
Sample Input5 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 +HINT
The first three days walk 1-4-5, after two days walk 1-3-5, so the total cost is () *3+ (3+2) *2+10=32
The puzzle: We can divide the period in days. C[j][i] said from Day J to the day I can continue to walk the shortest possible, we only need to do n^2 SPFA, there is a notable point is that we can use the array to record a prefix, can be relatively quick to determine whether the first day of the day I can be used.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib>using namespace std; int N,m,k,e,map[30][30],past[30][110],f[110],c[30][30],l[1000000],dis[30];bool ff[30];int SPFA (int x,int y) {int h (0) , T (1), u; memset (ff,false,sizeof (FF)); memset (dis,127/3,sizeof (dis)); l[t]=1;ff[1]=true;dis[1]=0; while (h<t) {u=l[++h];ff[u]=false; for (int i=1;i<=m;i++) if (dis[i]>dis[u]+map[u][i]&&map[u][i]&&past[i][y]-past[i][x]==y-x) {Dis[i]=dis[u]+map[u][i]; if (!ff[i]) {ff[i]=true; if (Dis[i]<dis[l[h+1]]) l[h--]=i; else l[++t]=i; }}}} return Dis[m]>100000000?dis[m]:d is[m]* (y-x);} int in () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} int main () { int x, y, Z; memset (F,127/3,sizeof (f)); f[0]=0; N=in (); M=in (); K=in (); E=in (); for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) past[i][j]=1; for (int i=1;i<=e;i++) {x=in (); Y=in (); Z=in (); Map[x][y]=map[y][x]=z; } e=in (); for (int i=1;i<=e;i++) {x=in (); Y=in (); Z=in (); for (int j=y;j<=z;j++) past[x][j]=0; } for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) past[i][j]+=past[i][j-1]; for (int i=1;i<=n;i++) for (int j=0;j<=i-1;j++) {int TEMP=SPFA (j,i); if (temp<10000000&& (f[j]+temp+k) <f[i]) f[i]=f[j]+temp+k; } printf ("%d\n", F[n]-k);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Bzoj1003" "ZJOI2006" "Logistics transport" "Shortest way +DP"