BZOJ 2330: [SCOI2011]糖果
来源:互联网 发布:百变小樱之知世 编辑:程序博客网 时间:2024/05/17 07:39
第一次写差分约束,收获很多,首先我们表示一个人一定要比另一个人少只需要从这个人向另一个人连一条边权为1的边,小于等于就连一条边权为0的边,等于就连两条,但是为什么求最小值却要求最大流呢?因为我们可以想象一下我们规定的这些限制条件在最小流时可能并不能得到满足(模拟一下便知)只有当求最大流时能满足所有限制条件得到一组可行解,因为我们没连没有用的其他边,所以这也是满足所有条件的最小值,然后题目中还有一个条件就是每个小朋友都必须分到糖果,我们只需要新建一个超级源点,让它向每个点都连一个1就可以了。
#include<cstdio>#include<cstdlib>#include<ctime>#include<cmath>#include<iostream>#include<iomanip>#include<cstring>#include<string>#include<algorithm>#include<queue>using namespace std;struct bian{ int l,r,v;}a[1000000];int fir[1000000];int nex[1000000];int tot=1;int dis[1000000];bool pd[1000000];int sum[1000000];void add_edge(int l,int r,int v){ a[++tot].l=l; a[tot].r=r; a[tot].v=v; nex[tot]=fir[l]; fir[l]=tot;}queue<int> dui;int n;bool spfa(){ memset(dis,0xef,sizeof(dis)); dis[0]=0; dui.push(0); pd[0]=true; while(!dui.empty()) { int u=dui.front(); dui.pop(); pd[u]=false; for(int o=fir[u];o;o=nex[o]) { if(dis[u]+a[o].v>dis[a[o].r]) { dis[a[o].r]=dis[u]+a[o].v; sum[a[o].r]=sum[u]+1; if(sum[a[o].r]>n+1) return false; if(!pd[a[o].r]) { pd[a[o].r]=true; dui.push(a[o].r); } } } } return true;}int main(){ int m; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int p,x,y; scanf("%d%d%d",&p,&x,&y); if(p==1) { add_edge(x,y,0); add_edge(y,x,0); } if(p==2) add_edge(x,y,1); if(p==3) add_edge(y,x,0); if(p==4) add_edge(y,x,1); if(p==5) add_edge(x,y,0); } for(int i=n;i>=1;i--) add_edge(0,i,1); if(!spfa()) cout<<-1; else { long long ans=0; for(int i=1;i<=n;i++) ans+=dis[i]; cout<<ans; } return 0;}
0 0
- BZOJ 2330: [SCOI2011]糖果
- BZOJ 2330 [SCOI2011]糖果
- BZOJ 2330: [SCOI2011]糖果
- BZOJ 2330: [SCOI2011]糖果
- BZOJ 2330: [SCOI2011]糖果
- BZOJ 2330[SCOI2011]糖果
- BZOJ 2330 [SCOI2011]糖果
- BZOJ 2330 SCOI2011 糖果 差分约束
- 【差分约束】BZOJ 2330: [SCOI2011]糖果
- 【bzoj 2330】[SCOI2011]糖果 差分约束
- 【BZOJ 2330】 [SCOI2011]糖果【差分约束】
- BZOJ 2330: [SCOI2011]糖果 查分约束
- |BZOJ 2330|差分约束|[SCOI2011]糖果
- BZOJ 2330 [SCOI2011]糖果 差分约束
- BZOJ 2330: [SCOI2011]糖果 差分约束
- BZOJ P2330[SCOI2011]糖果
- BZOJ 2330 [SCOI2011]糖果 差分约束spfa版
- bzoj 2330 [SCOI2011]糖果(差分约束系统)
- State Estimation for Robotics_2.1.1_Probability Density Functions: Definitions
- FFMPEG的一些使用技巧
- 小程序发布,你很方?
- Android studio Maven仓库使用
- cookie 和session 的区别详解
- BZOJ 2330: [SCOI2011]糖果
- 深度学习训练调优秘籍
- 【房价网房价信息爬虫】整站40万条房价数据并行抓取,可更换抓取城市
- FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
- 安卓material design 主题调色介绍
- CentOS忘记密码
- linux alias写快捷键笔记
- 17 ways to obtain your latest Vulnerabilities/Advisories/Exploits elsewhere
- C#获取当前日期时间