Burn the Linked Camp(bellman 差分约束系统)

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, 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 }
				
时间: 2024-08-23 04:22:54

Burn the Linked Camp(bellman 差分约束系统)的相关文章

ZOJ2770 Burn the Linked Camp(差分约束系统)

区间和一定要联系到前缀和. 这题,把前缀和看作点,从s0到sn: 对于每一个营地i的容量capi,有这么个关系si-si-1<=capi: 对于每一个区间的评估i,j,k,有sj-si-1>=k,即si-1-sj<=k: 接下来就是连边了,对于v<=u+w由u向v连权w的边,超级源向n+1个点连权0的边. 最后跑SPFA,如果出现负环则无解:而有解的话,所求答案就在d[sn]里,不过因为题目的前缀和是非负整数且要求的最少,所以就要让d中所有数都同时加上合适的数得到另一个真正所求的解

ZOJ 2770 Burn the Linked Camp( 差分约束啊 )

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1770 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'

ZOJ 2770 Burn the Linked Camp 差分约束

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2770 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, was defeate

zoj2770 Burn the Linked Camp --- 差分约束

有n个营地,每个营地至多容纳Ci人,给出m个条件:第i到第j个营地之间至少有k人. 问n个营地总共至少有多少人. 此题显然差分约束,要求最小值,则建立x-y>=z方程组,建图求最长路. 用d[i]表示[1,i]个帐篷中一共多少人,根据题意可得到不等关系: 1.0<=d[i]-d[i-1]<=C[i] 2.d[j]-d[i]>=k 此外,我们添加0为附加结点,则0到其他点也要建边. 再求解0为源点的最长路即可. 我的坑点是,判负环返回0,否则返回d[n]. 而d[n]本身就可能是0.

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA

第一道正儿八经的差分约束题 有排成一列的n个点,首先告诉你每个点的值最多是多少(最少显然要大于0),然后告诉你m段i,j,k,表示第i个点到第j个点的值的和至少有k,问你总和至少为多少. 要注意的是,告诉你的所有关系式都不要忘记建边,一开始漏了大于0的条件调半天o(╯□╰)o 不等式的形式是a-b<=c这样的= = 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <

zoj 2770 Burn the Linked Camp

今天刚刚学差分约束系统.利用最短路求解不等式.世界真的好奇妙!感觉不等式漏下几个会导致WA!! #include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; const int maxn = 1111; vector<int>ljb[ma

ZOJ 2770 Burn the Linked Camp 差分约束 (转)

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 ca

ZOJ– 2770Burn the Linked Camp单源最短路径差分约束系统

ZOJ Problem Set – 2770 Burn the Linked Camp 题目的大意为: 陆逊侦查得知刘备将自己的军队分为N个营,从左到右编号为1,2,3…N.第i个营有最大士兵数Ci,通过一段时间的观察,陆逊可以估算至少有k个士兵住在第i到第j个营地,最后陆逊必须要估算刘备的军队至少有多少士兵,以便应对. 输入: 有多个测试案例,在每个测试案例的第一行,有两个整数N(0<n<=1000)和M(0 <= M = 10000).第二行中,有n个整数C1…CN.然后M行,每行有

ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)

当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方程组-转换为最短路问题 //d[v] <= d[u] + dis[u][v] -> d[v] - d[u] <= dis[u][v] //Time:110Ms Memory:12116 #include<iostream> #include<cstring> #inc