洛谷 2057_善意的投票_网络流
来源:互联网 发布:深圳公务员 知乎 编辑:程序博客网 时间:2024/05/29 09:12
题目描述
幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的人数。
我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?
思路
将投同意的和源点0连边,将不同意的和汇点n+1连边
对于每一个好朋友都连一个双向边
跑最大流就可以了
#include <stdio.h>#include <string.h>#include <queue>#define maxn 400000#define min(x,y) x<y?x:y#define INF 0x7f7f7f7f#define fill(x,i) memset(x,i,sizeof(x))using namespace std;struct edge{int y,w,next;}e[maxn];int ls[maxn],n,m,maxE=1,S,E,state[maxn];bool vis[maxn];inline int read(){ int x=0,p=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-')p=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();} return x*p;}int add(int x,int y,int w){ e[++maxE]=(edge){y,w,ls[x]}; ls[x]=maxE; e[++maxE]=(edge){x,0,ls[y]}; ls[y]=maxE;}int bfs(int x){ queue <int> t; t.push(x); fill(state,0); state[x]=1; while (!t.empty()) { int tt=t.front();t.pop(); for (int i=ls[tt];i;i=e[i].next) { if (e[i].w>0&&!state[e[i].y]) { state[e[i].y]=state[tt]+1; t.push(e[i].y); if (e[i].y==E) return true; } } } return false;}int find(int x,int mn){ if (x==E||!mn) return mn; int ret=0; for (int i=ls[x];i;i=e[i].next) if (state[x]+1==state[e[i].y]&&e[i].w>0) { int d=find(e[i].y,min(mn-ret,e[i].w)); e[i].w-=d; e[i^1].w+=d; ret+=d; if (ret==mn) break; } return ret;}int main(){ int n=read(),m=read(); S=0;E=n+1; for (int i=1;i<=n;i++) { int x=read(); if (x==1) add(S,i,1); else add(i,E,1); } for (int i=1;i<=m;i++) { int x=read(),y=read(); add(x,y,1); add(y,x,1); } int ans=0; while (bfs(S)) ans+=find(S,INF); printf("%d\n",ans);}
1 0
- 洛谷 2057_善意的投票_网络流
- 1934: [Shoi2007]Vote 善意的投票 (网络流)
- 洛谷 P2057 善意的投票
- 洛谷 P2057 善意的投票
- 【SHOI2009】善意的投票
- BZOJ1934Vote 善意的投票
- luogu2057 善意的投票
- 【SHOI2007】bzoj1934 善意的投票
- BZOJ-1934 Vote 善意的投票 最大流+建图
- 教辅的组成_洛谷1231_网络流
- 洛谷 1231_教辅的组成_网络流
- 【bzoj1934】 [Shoi2007]Vote 善意的投票
- BZOJ 1934: [Shoi2007]Vote 善意的投票
- bzoj 1934: [Shoi2007]Vote 善意的投票
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
- BZOJ-1934-Vote善意的投票-SHOI2007
- bzoj1934【shoi2007】Vote善意的投票
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
- 面向对象特性
- 微服务架构模式简介
- cordova 安装及使用
- Java 垃圾回收和注意事项
- 探索并发编程(三)------Java存储模型和共享对象
- 洛谷 2057_善意的投票_网络流
- SQLite数据库
- [深度学习] TensorFlow 入门之基本使用
- shell 基本用法---持续更新中
- Linux 命令格式
- 求连通分量(DFS)
- PHP-CPP 安装
- BIM特点及格式文件说明
- STM and MVCC considerations