Burn the Linked Camp Time limit: 2 Seconds Memory Limit: 65536 KB
It is well known that, in the period of the three Empires, Liu Bei, the emperor of the Shu Empire, were defeated by Lu Xun, A general of the Wu Empire. The defeat is 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 is the so-called "Linked camps".
Let's go back to that time. Lu Xun had sent many scouts to obtain the information on his enemy. From him 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 do Liu Bei had, so the he could decide how many troops he Shoul D Send to burn Liu Bei ' s Linked camps.
Input:
There is multiple test cases! On the first line of all test case, there is integers n (0<n<=1,000) and M (0<=m<=10,000). On the second line, there is n integers C1?? Cn. Then M. lines follow, each of the line have three integers I, J, K (0<i<=j<=n, 0<=k<2^31), meaning that the total nu Mber of soldiers from the ith camp to the jth camp are at least K.
Output:
For each test case, the 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 is very unprecise. If His estimations cannot is true, output "bad estimations" with a single line instead.
Sample Input:
3 21000 2000 10001 2 11002 3 13003 1100 200 3002 3 600
Sample Output:
1300Bad estimations
Note that DIJ can only deal with the shortest-path problem of nonnegative weights, so the differential constraint system is used Bellman ()
Note: The topic should be done with SPFA, otherwise the Bellman O (nm) algorithm is easy to time out, but this problem not only has not timed out, unexpectedly can be passed within 100ms of time.
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>Ten#include <queue> One#include <stack> A#include <sstream> -#include <iomanip> - using namespacestd; the Const intinf=0x4fffffff; - Const intexp=1e-6; - Const intms=1005; - + structEdge - { + intu,v,w; A}edges[ +*MS]; at - intn,m,esize; - intDis[ms]; - intC[ms]; - intMaxv[ms]; - in intinput () - { to if(SCANF ("%d%d", &n,&m) = =EOF) + return 0; -Fill (dis,dis+ms,inf); thedis[n]=0; *memset (MAXV,0,sizeof(MAXV)); $Esize=0;Panax Notoginseng for(intI=1; i<=n;i++) - { thescanf"%d",&c[i]); +maxv[i]=c[i]+maxv[i-1]; Aedges[esize].u=i-1; theedges[esize].v=i; +edges[esize++].w=C[i]; - $edges[esize].u=i; $edges[esize].v=i-1; -edges[esize++].w=0; - } the intu,v,w; - for(intI=0; i<m;i++)Wuyi { thescanf"%d%d%d",&u,&v,&W); -edges[esize].u=v; Wuedges[esize].v=u-1; -edges[esize++].w=-W; About $edges[esize].u=u-1; -edges[esize].v=v; -edges[esize++].w=maxv[v]-maxv[u-1]; - } A return 1; + } the - BOOLBellman () $ { the for(intI=0; i<=n;i++)//pay attention to the number of fixed points is n+1, otherwise it will be wrong. the { the for(intj=0; j<esize;j++) the { - if(dis[edges[j].u]+edges[j].w<DIS[EDGES[J].V]) in { thedis[edges[j].v]=dis[edges[j].u]+EDGES[J].W; the if(i==N) About return false; the } the } the } + return true; - } the Bayi intMain () the { the while(Input ()) - { - if(Bellman ()) theprintf"%d\n", dis[n]-dis[0]); the Else theprintf"Bad estimations\n"); the } - return 0; the}
Burn the Linked Camp (bellman differential constraint system)