bzoj2330 [SCOI2011]糖果

题目链接

差分约束系统

注意每人至少一颗糖,所以从S连出去的边边权要为1

注意在输入的时候特判不然会T

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<string>
 7 #include<cmath>
 8 #include<ctime>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
15 #define Clear(a,b) memset(a,b,sizeof(a))
16 #define inout(x) printf("%d",(x))
17 #define douin(x) scanf("%lf",&x)
18 #define strin(x) scanf("%s",(x))
19 #define LLin(x) scanf("%lld",&x)
20 #define op operator
21 #define CSC main
22 typedef unsigned long long ULL;
23 typedef const int cint;
24 typedef long long LL;
25 using namespace std;
26 void inin(int &ret)
27 {
28     ret=0;int f=0;char ch=getchar();
29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
31     ret=f?-ret:ret;
32 }
33 int n,k;
34 int head[100010],next[300020],zhi[300020],w[300020],ed;
35 void add(int a,int b,int c)
36 {
37     next[++ed]=head[a],head[a]=ed,zhi[ed]=b,w[ed]=c;
38 //    next[++ed]=head[b],head[b]=ed,zhi[ed]=a,w[ed]=c;
39 }
40 queue<int>h;bool bo[100010];int dis[100010],shu[100010];
41 bool spfa(int s)
42 {
43     h.push(s);shu[s]=1;
44     while(!h.empty())
45     {
46         int x=h.front();h.pop();bo[x]=0;
47         for(int i=head[x];i;i=next[i])if(dis[zhi[i]]<dis[x]+w[i])
48         {
49             if(++shu[zhi[i]]>=n)return 0;
50             dis[zhi[i]]=dis[x]+w[i];
51             if(!bo[zhi[i]])h.push(zhi[i]),bo[zhi[i]]=1;
52         }
53     }
54     return 1;
55 }
56 int CSC()
57 {
58     inin(n),inin(k);
59     re(i,1,k)
60     {
61         int opt,q,w;
62         inin(opt),inin(q),inin(w);
63         if(opt==1)add(q,w,0),add(w,q,0);
64         else if(opt==2)
65             if(q==w){printf("-1");return 0;}
66             else add(q,w,1);
67         else if(opt==3)add(w,q,0);
68         else if(opt==4)
69             if(q==w){printf("-1");return 0;}
70             else add(w,q,1);
71         else if(opt==5)add(q,w,0);
72     }
73     int s=100001;
74     rre(i,n,1)add(s,i,1);
75     if(!spfa(s)){printf("-1");return 0;}
76     LL ans=0;
77     re(i,1,n)ans+=dis[i];
78     printf("%lld",ans);
79     return 0;
80 }

 

posted @ 2016-02-19 15:59  HugeGun  阅读(172)  评论(1编辑  收藏  举报