bzoj2330: [SCOI2011]糖果
来源:互联网 发布:如何整理mac里的照片 编辑:程序博客网 时间:2024/05/29 03:44
差分约束
今天又一次学习了一波,
才发现这种问题可以转化为图论,好神奇233
再次证明我好菜2333
#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>#include<queue>using namespace std;int n,k;int f[100005];int to[300005],_next[300005],head[100005],v[300005];int cir[100005],e[100005];int cnt=0;long long ans=0;queue<int>que;void add(int x,int y,int z){ cnt++; to[cnt]=y; _next[cnt]=head[x]; head[x]=cnt; v[cnt]=z;}bool lcr(){ int i,j,k,l; que.push(0); cir[0]=1; e[0]=1; while(!que.empty()){ k=que.front(); for(i=head[k];i;i=_next[i]) if(f[to[i]]<f[k]+v[i]){ f[to[i]]=f[k]+v[i]; cir[to[i]]++; if(cir[to[i]]>n)return true; if(!e[to[i]]){ e[to[i]]=1; que.push(to[i]); } } que.pop(); e[k]=0; } return false;} int main(){ int i,j,l,x,a,b; scanf("%d%d",&n,&k); for(i=1;i<=k;i++){ scanf("%d%d%d",&x,&a,&b); if(x==1){add(a,b,0);add(b,a,0);} if(x==2){if(a==b){printf("-1");return 0;}add(a,b,1);} if(x==3){add(b,a,0);} if(x==4){if(a==b){printf("-1");return 0;}add(b,a,1);} if(x==5){add(a,b,0);} } for(i=n;i>=1;i--)add(0,i,1); if(lcr()){printf("-1");return 0;} for(i=1;i<=n;i++)ans+=f[i]; printf("%lld",ans); return 0;}
0 0
- bzoj2330 [SCOI2011]糖果题解
- [BZOJ2330][SCOI2011]糖果
- [BZOJ2330][SCOI2011]糖果
- bzoj2330 [SCOI2011]糖果
- bzoj2330【SCOI2011】糖果
- 12.7 bzoj2330 [SCOI2011]糖果
- [题解]bzoj2330(SCOI2011)糖果
- 【bzoj2330】[SCOI2011]糖果
- bzoj2330: [SCOI2011]糖果
- 【bzoj2330】[SCOI2011]糖果
- bzoj2330[SCOI2011]糖果
- bzoj2330 [SCOI2011]糖果
- 【bzoj2330】【P3275 】【SCOI2011】糖果
- bzoj2330: [SCOI2011]糖果
- BZOJ2330: [SCOI2011]糖果
- [SCOI2011]糖果 bzoj2330 洛谷 P3275
- bzoj2330 [SCOI2011]糖果 差分约束
- 【差分约束】[SCOI2011] bzoj2330 糖果
- 文章标题
- 【MySQL基础】数据的备份与还原
- Bitmap毛边问题
- 动态规划 16 (踩方格)
- 图像反亮
- bzoj2330: [SCOI2011]糖果
- 算法分析与设计第九周:45. Jump Game II
- MATLAB遗传算法例子一
- new delete
- mysqli_connect() 2005错误
- 暗黑破坏神(DIABLOII 1.11B)BOT制作(一)
- 第一天作业
- 【DOM】2.改变新闻字体大小
- CCFCSP201612-1中间数