BZOJ 1934 善意的投票(最小割)
来源:互联网 发布:淘宝行业关键词转化率 编辑:程序博客网 时间:2024/04/30 11:05
建立一个源点S和一个汇点T,同意的向S连边,不同意的向T连边,每对朋友之间互相连边,然后跑一遍最大流算法。
#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <algorithm>#include <queue>#include <vector>#include <cmath>#define LL long longusing namespace std;const int maxn = 1000 + 10;const int INF = 10000000;struct Edge{int from,to,cap,flow;Edge(int u,int v,int c,int f) : from(u),to(v),cap(c),flow(f) { }};int N , M;vector<Edge>edges;vector<int>G[maxn];int a[maxn];int p[maxn];void init(int n){for(int i=0;i<=n;i++)G[i].clear();edges.clear();}void AddEdge(int from,int to,int cap){edges.push_back(Edge(from,to,cap,0));edges.push_back(Edge(to,from,0,0));int m = edges.size();G[from].push_back(m-2);G[to].push_back(m-1);}int Maxflow(int s,int t){int flow = 0;for(;;){memset(a,0,sizeof(a));queue<int>Q;Q.push(s);a[s] = INF;while(!Q.empty()){int x = Q.front();Q.pop();for(int i=0;i<G[x].size();i++){Edge& e = edges[G[x][i]];if(!a[e.to] && e.cap > e.flow){p[e.to] = G[x][i];a[e.to] = min(a[x],e.cap-e.flow);Q.push(e.to);}}if(a[t]) break;}if(!a[t]) break;for(int u=t;u!=s;u = edges[p[u]].from){edges[p[u]].flow += a[t];edges[p[u]^1].flow -= a[t];}flow += a[t];}return flow;}int main(){while(scanf("%d%d",&N,&M)!=EOF){int tmp;for(int i=1;i<=N;i++){scanf("%d",&tmp);if(tmp == 1) AddEdge(0,i,1);else AddEdge(i,N+1,1);}int u , v;while(M--){scanf("%d%d",&u,&v);AddEdge(u,v,1);AddEdge(v,u,1);}int ans = Maxflow(0,N+1);printf("%d\n",ans);}return 0;}
0 0
- BZOJ 1934 善意的投票(最小割)
- bzoj 1934: [Shoi2007]Vote 善意的投票(最小割)
- BZOJ 1934([Shoi2007]Vote 善意的投票-最小割)
- 【bzoj 1934】[Shoi2007]Vote 善意的投票 最小割
- [BZOJ1934][Shoi2007]Vote 善意的投票(最小割)
- bzoj1934 [Shoi2007]Vote 善意的投票(最小割)
- bzoj1934: [Shoi2007]Vote 善意的投票 最小割
- BZOJ_P1934 [Shoi2007]Vote 善意的投票(最小割)
- 【BZOJ1934】【codevs2341】善意的投票,二分图最小割
- [SHOI2007]BZOJ1934 Vote善意的投票-最小割
- [bzoj1934][SHOI2007] Vote 善意的投票 最小割
- BZOJ1934 [Shoi2007]Vote 善意的投票 【最小割】
- BZOJ 1934: [Shoi2007]Vote 善意的投票
- bzoj 1934: [Shoi2007]Vote 善意的投票
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
- BZOJ-1934-Vote善意的投票-SHOI2007
- 【BZOJ 1934】 [Shoi2007]Vote 善意的投票
- BZOJ 1934 [Shoi2007]Vote 善意的投票
- Rackspace 将支持更多的开源数据库
- 工厂模式的Assembly.Load(path).CreateInstance(className)出错解决方法
- 设计模式之 ----------- 迭代器模式
- 破茧成蝶1:设计流程——设计师具体做什么?
- 昂贵的聘礼(最短路)
- BZOJ 1934 善意的投票(最小割)
- POJ 2299 Ultra-QuickSort(归并排序求逆序对数)
- Java将毫秒转成时分秒,在android中常用
- 截图
- 杭电1284 钱换为3,2,1分的硬币,换取的方法有多少种类
- Watch The Movie
- 关于Ubuntu配置文件apache2 httpd.conf位置
- VMvare 虚拟网卡vmnet1 and vmnet8
- VS2013编译libhackrf以及测试