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 <algorithm>
 5 #include <climits>
 6 #include <string>
 7 #include <iostream>
 8 #include <map>
 9 #include <cstdlib>
10 #include <list>
11 #include <set>
12 #include <queue>
13 #include <stack>
14
15 using namespace std;
16
17 typedef long long LL;
18 const int maxn = 1000 + 5;
19 const int maxm = 50000 + 5;
20 const LL INF = LONG_LONG_MAX / 4;
21 int v[maxm],w[maxm],first[maxn],nxt[maxm];
22 int n,m,C,cnt[maxn],ecnt;
23 LL d[maxn];
24 bool inq[maxn];
25
26 void solve() {
27     bool bad = false;
28     queue<int> q;
29     q.push(n);
30     for(int i = 0;i <= n;i++) {
31         inq[i] = false;
32         d[i] = INF;
33         cnt[i] = 0;
34     }
35     d[n] = 0;
36     inq[n] = true;
37     cnt[n] = 1;
38     while(!q.empty() && !bad) {
39         int x = q.front(); q.pop();
40         inq[x] = false;
41         for(int i = first[x];i != -1;i = nxt[i]) {
42             if(d[v[i]] > d[x] + w[i]) {
43                 d[v[i]] = d[x] + w[i];
44                 if(!inq[v[i]]) {
45                     q.push(v[i]);
46                     cnt[v[i]]++;
47                     inq[v[i]] = true;
48                     if(cnt[v[i]] > n + 1) {
49                         bad = true; break;
50                     }
51                 }
52             }
53         }
54     }
55     if(bad) puts("Bad Estimations");
56     else printf("%lld\n",-d[0]);
57 }
58
59 void adde(int _u,int _v,int _w) {
60     v[ecnt] = _v; w[ecnt] = _w;
61     nxt[ecnt] = first[_u];
62     first[_u] = ecnt;
63     ecnt++;
64 }
65
66 int main() {
67     while(~scanf("%d%d",&n,&m)) {
68         ecnt = 0;
69         memset(first,-1,sizeof(first));
70         memset(nxt,-1,sizeof(nxt));
71         for(int i = 1;i <= n;i++) {
72            scanf("%d",&C);
73            adde(i - 1,i,C);
74            adde(i,i - 1,0);
75         }
76         for(int i = 1;i <= m;i++) {
77             int a,b,c; scanf("%d%d%d",&a,&b,&c);
78             adde(b,a - 1,-c);
79         }
80         solve();
81     }
82     return 0;
83 }

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

时间: 2024-10-10 07:17:27

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA的相关文章

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

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

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( 差分约束啊 )

题目链接: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

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

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 Be

ZOJ 2770 差分约束+SPFA

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 Be

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc