标签:
It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a general of the Wu Empire. The defeat was due to Liu Bei‘s wrong decision that he divided his large troops into a number of camps, each of which had a group of armies, and located them in a line. This was the so-called "Linked Camps".
Let‘s go back to that time. Lu Xun had sent many scouts to obtain the information about his enemy. From his scouts, he knew that Liu Bei had divided his troops into n camps, all of which located in a line, labeled by 1..n from left to right. The ith camp had a maximum capacity of Ci soldiers. Furthermore, by observing the activities Liu Bei‘s troops had been doing those days, Lu Xun could estimate the least total number of soldiers that were lived in from the ith to the jth camp. Finally, Lu Xun must estimate at least how many soldiers did Liu Bei had, so that he could decide how many troops he should send to burn Liu Bei‘s Linked Camps.
Input:
There are multiple test cases! On the first line of each test case, there are two integers n (0<n<=1,000) and m (0<=m<=10,000). On the second line, there are n integers C1??Cn. Then m lines follow, each line has three integers i, j, k (0<i<=j<=n, 0<=k<2^31), meaning that the total number of soldiers from the ith camp to the jth camp is at least k.
Output:
For each test case, output one integer in a single line: the least number of all soldiers in Liu Bei‘s army from Lu Xun‘s observation. However, Lu Xun‘s estimations given in the input data may be very unprecise. If his estimations cannot be true, output "Bad Estimations" in a single line instead.
Sample Input:
3 2 1000 2000 1000 1 2 1100 2 3 1300 3 1 100 200 300 2 3 600
Sample Output:
1300 Bad Estimations
注意dij只能处理非负权值的最短路问题, 所以差分约束系统一律用 bellman()
注意:本题应该是用spfa做的,不然bellman O(nm)算法很容易超时, 但此题不但没有超时,竟然能在100ms的时间内通过。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <queue> 11 #include <stack> 12 #include <sstream> 13 #include <iomanip> 14 using namespace std; 15 const int INF=0x4fffffff; 16 const int EXP=1e-6; 17 const int MS=1005; 18 19 struct edge 20 { 21 int u,v,w; 22 }edges[40*MS]; 23 24 int n,m,esize; 25 int dis[MS]; 26 int C[MS]; 27 int maxv[MS]; 28 29 int input() 30 { 31 if(scanf("%d%d",&n,&m)==EOF) 32 return 0; 33 fill(dis,dis+MS,INF); 34 dis[n]=0; 35 memset(maxv,0,sizeof(maxv)); 36 esize=0; 37 for(int i=1;i<=n;i++) 38 { 39 scanf("%d",&C[i]); 40 maxv[i]=C[i]+maxv[i-1]; 41 edges[esize].u=i-1; 42 edges[esize].v=i; 43 edges[esize++].w=C[i]; 44 45 edges[esize].u=i; 46 edges[esize].v=i-1; 47 edges[esize++].w=0; 48 } 49 int u,v,w; 50 for(int i=0;i<m;i++) 51 { 52 scanf("%d%d%d",&u,&v,&w); 53 edges[esize].u=v; 54 edges[esize].v=u-1; 55 edges[esize++].w=-w; 56 57 edges[esize].u=u-1; 58 edges[esize].v=v; 59 edges[esize++].w=maxv[v]-maxv[u-1]; 60 } 61 return 1; 62 } 63 64 bool bellman() 65 { 66 for(int i=0;i<=n;i++) //千万注意定点个数是n+1,不然会错。 67 { 68 for(int j=0;j<esize;j++) 69 { 70 if(dis[edges[j].u]+edges[j].w<dis[edges[j].v]) 71 { 72 dis[edges[j].v]=dis[edges[j].u]+edges[j].w; 73 if(i==n) 74 return false; 75 } 76 } 77 } 78 return true; 79 } 80 81 int main() 82 { 83 while(input()) 84 { 85 if(bellman()) 86 printf("%d\n",dis[n]-dis[0]); 87 else 88 printf("Bad Estimations\n"); 89 } 90 return 0; 91 }
Burn the Linked Camp(bellman 差分约束系统)
标签:
原文地址:http://www.cnblogs.com/767355675hutaishi/p/4374785.html