Uvalive 2957 Bring Them There
来源:互联网 发布:java重要的知识点总结 编辑:程序博客网 时间:2024/04/29 08:00
拆点网络流,方法很巧妙。
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<cmath>#include<stack>#include<cmath>#include<set>#include<map>#define LL long long#define CLR(a, b) memset(a, b, sizeof(a))using namespace std;const int maxn = 2020;const int INF = 0x3f3f3f3f;struct Edge{ int from, to, cap, flow; Edge() {} Edge(int from, int to, int cap, int flow) :from(from), to(to), cap(cap), flow(flow) {}};struct ISAP{ int n, m, s, t; vector<Edge> edges; vector<int> G[maxn]; bool vis[maxn]; int d[maxn], cur[maxn]; int p[maxn], num[maxn]; void init() { edges.clear(); } void ClearEdge(int bn, int n) { this->n = n; for(int i = bn; i < n; i ++) G[i].clear(); } void ClearFlow() { for(int i = 0; i < edges.size(); i ++) edges[i].flow = 0; } void AddEdge(int from, int to, int cap) { edges.push_back(Edge(from, to, cap, 0)); edges.push_back(Edge(to, from, 0, 0)); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { CLR(vis, false); queue<int> Q; Q.push(t); vis[t] = 1; d[t] = 0; while(!Q.empty()) { int x = Q.front(); Q.pop(); for(int i = 0; i < G[x].size(); i ++) { Edge& e = edges[G[x][i]^1]; if(!vis[e.from] && e.cap > e.flow) { vis[e.from] = true; d[e.from] = d[x] + 1; Q.push(e.from); } } } return vis[s]; } int Augment() { int x = t, a = INF; while(x != s) { Edge& e = edges[p[x]]; a = min(a, e.cap-e.flow); x = edges[p[x]].from; } x = t; while(x != s) { edges[p[x]].flow += a; edges[p[x]^1].flow -= a; x = edges[p[x]].from; } return a; } int Maxflow(int s, int t, int limit) { this->s = s; this->t = t; int flow = 0; BFS();CLR(num, 0); for(int i = 0; i < n; i ++) num[d[i]] ++; int x = s; CLR(cur, 0); while(d[s] < n) { if(x == t) { flow += Augment(); if(flow >= limit) return flow; x = s; } int ok = 0; for(int i = cur[x]; i < G[x].size(); i ++) { Edge& e = edges[G[x][i]]; if(e.cap > e.flow && d[x] == d[e.to] + 1) { ok = 1; p[e.to] = G[x][i]; cur[x] = i; x = e.to; break; } } if(!ok) { int m = n - 1; for(int i = 0; i < G[x].size(); i ++) { Edge& e = edges[G[x][i]]; if(e.cap > e.flow) m = min(m, d[e.to]); } if(-- num[d[x]] == 0) break; num[d[x] = m + 1] ++; cur[x] = 0; if(x != s) x = edges[p[x]].from; } } return flow; } void Reduce() { for(int i = 0; i < edges.size(); i ++) edges[i].cap -= edges[i].flow; } vector<int> Mincut() { vector<int> ans; BFS(); for(int i = 0; i < edges.size(); i ++) { Edge e = edges[i]; if(!vis[e.from] && vis[e.to] && e.cap > 0) ans.push_back(i); } return ans; }} sol;vector<pair<int, int> > path, ans;int main(){ int n, m, k, s, t; while(scanf("%d%d%d%d%d", &n, &m, &k, &s, &t) != EOF) { s --; t --; path.clear(); for(int i = 0; i < m; i ++) { int u, v; scanf("%d%d", &u, &v); path.push_back(make_pair(-- u, -- v)); } int day = 0, maxflow = 0; sol.init();sol.ClearEdge(0, n); while(maxflow < k) { int last = day * n, now = last + n; sol.ClearEdge(now, now + n); for(int i = 0; i < n; i ++) sol.AddEdge(last + i, now + i, INF); for(int i = 0; i < m; i ++) { int u = path[i].first, v = path[i].second; sol.AddEdge(last + u, now + v, 1); sol.AddEdge(last + v, now + u, 1); } maxflow += sol.Maxflow(s, t + now, k - maxflow); day ++; } printf("%d\n", day); vector<int> loc(k, s); for(int i = 0, j = 0; i < day; i ++) { j += 2 * n; ans.clear(); while(j < (i + 1) * (2 * n + 4 * m)) { bool f1 = false, f2 = false; Edge e1 = sol.edges[j]; if(e1.flow == 1) f1 = true; j += 2; Edge e2 = sol.edges[j]; if(e2.flow == 1) f2 = true; j += 2; if(f1 && !f2) ans.push_back(make_pair(e1.from % n, e1.to % n)); if(!f1 && f2) ans.push_back(make_pair(e2.from % n, e2.to % n)); } printf("%d", ans.size()); vector<int> mov(k, 0); for(int i = 0; i < ans.size(); i ++) { int u = ans[i].first, v = ans[i].second; for(int j = 0; j < k; j ++) { if(loc[j] == u && !mov[j]) { mov[j] = 1; loc[j] = v; printf(" %d %d", j + 1, v + 1); break; } } }puts(""); } }}
0 0
- Uvalive 2957 Bring Them There
- uvalive 2957 Bring Them There(最大流)
- UVALive 2957 Bring Them There(拆点+最大流)
- UVALive 2957Bring Them There (最大流+拆点)
- UVALive - 2957 Bring Them There(最大流 图论建模)
- POJ-1895-Bring Them There
- uva1324 - Bring Them There 网络流
- UVA 1324 Bring Them There 拆点+打印解
- uva 1324 Bring Them There (最大流)
- 【poj1895】Bring Them There 分层图最大流
- [POJ]1895 Bring Them There 网络流 动态加层
- POJ 1895 Bring Them There 分层构图 求最大流 并输出路径
- 运送超级计算机 分裂点 网络流 LA2957/UVa1324 Bring Them There
- UVALive 3882 And Then There Was One
- UVALive 3882 And Then There Was One
- UVALive 3882 And Then There Was One
- And Then There Was One UVALive
- UVALive 3882 And Then There Was One 约瑟夫环问题
- apache cxf spring 用户加密
- PS图层混合算法之一(不透明度,正片叠底,颜色加深,颜色减淡)
- HDU 1863 畅通工程(Kruskal + 并查集)
- 智慧全媒体数字报免费下载
- 武汉JAVA/android培训最优选择--传智播客武汉分校!
- Uvalive 2957 Bring Them There
- C# 关于泛型
- 疑似iPhone 6后盖计划图曝光 屏幕标准更大
- 等长字符串排序
- Hadoop工作机制
- 最近面试的一些事
- javascript的for()语句新用法--应急时获取未知对象信息
- cocos2dx socket 多线程使用
- 润乾——导出pdf文件调用jar包说明