POJ 2987 Firing 最大权闭合子图
来源:互联网 发布:如何卸载2008数据库 编辑:程序博客网 时间:2024/06/05 15:09
参考资料: 《最小割模型在信息学竞赛中的应用》
最大权闭合子图:
若一有向图G的一个子V图满足 V中所有点的出边仍指向V中的点,则该子图V就是G的一个闭合子图,其中V的顶点权和最大的称为G的最大权闭合子图
上图中闭合图有:{5}、{2,5}、 {2,4,5}、{3,4,5}、 {1,2,3,4,5}、{1,2,4,5}
最大权闭合子图为 {3,4,5}
建图方法:
新加源点S和汇点T,对于所有权值大于0的点,连一条从S到该点的边,容量为该点的权值,对于所有权值小于0的点,连一条从该点到T的边,容量为该点的权值的绝对值,对于原图中的边,建边,容量为INF,如下图所示。
最大权闭合子图的权值 = 所有正权点和 - 最大流
以割为界,靠近S一侧的点属于最大权闭合子图中的点
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;#define REP(i, a, b) for(int i = a; i < b; i++)#define FOR(i, a, b) for(int i = a; i <= b; i++)#define CLR(a, x) memset(a, x, sizeof(a))#define bug puts("***bug***")typedef long long LL;const int maxn = 5000 + 10;const int INF = 1e9;struct Edge{ int u, v, cap, flow; Edge(){} Edge(int u, int v, int cap, int flow) : u(u), v(v), cap(cap), flow(flow){}};struct ISAP{ int n, s, t; int cur[maxn], d[maxn], p[maxn], num[maxn], vis[maxn]; vector<int> G[maxn]; vector<Edge> edges; void init(int n){ this -> n = n; REP(i, 0, n) G[i].clear(); edges.clear(); CLR(vis, 0); } void add(int u, int v, int cap){ edges.push_back(Edge(u, v, cap, 0)); edges.push_back(Edge(v, u, 0, 0)); int m = edges.size(); G[u].push_back(m-2); G[v].push_back(m-1); } void bfs(){ REP(i, 0, n) d[i] = INF; d[t] =0; queue<int> Q; Q.push(t); while(!Q.empty()){ int x = Q.front(); Q.pop(); REP(i, 0, G[x].size()){ Edge &e = edges[G[x][i]]; if(e.cap > 0 || d[e.v] <= d[x] + 1) continue; d[e.v] = d[x] + 1; Q.push(e.v); } } } int augment(){ int x = t, a = INF; while(x != s){ Edge &e = edges[p[x]]; a = min(a, e.cap - e.flow); x = e.u; } x = t; while(x != s){ edges[p[x]].flow += a; edges[p[x]^1].flow -= a; x = edges[p[x]].u; } return a; } LL maxflow(int s, int t){ this -> s = s; this -> t = t; CLR(cur, 0); CLR(num, 0); bfs(); REP(i, 0, n) if(d[i] != INF) num[d[i]]++; LL flow = 0; int x = s; while(d[s] < n){ if(x == t){ flow += (LL)augment(); x = s; } int ok = 0; REP(i, cur[x], G[x].size()){ Edge &e = edges[G[x][i]]; if(e.cap > e.flow && d[e.v] + 1 == d[x]){ ok = 1; cur[x] = i; p[e.v] = G[x][i]; x = e.v; break; } } if(!ok){ int m = n - 1; REP(i, 0, G[x].size()){ Edge &e = edges[G[x][i]]; if(e.cap > e.flow) m = min(m, d[e.v]); } if(--num[d[x]] == 0) break; ++num[d[x] = m + 1]; cur[x] = 0; if(x != s) x = edges[p[x]].u; } } return flow; } void dfs(int x){ vis[x] = 1; REP(i, 0, G[x].size()){ Edge &e = edges[G[x][i]]; if(e.cap > e.flow && !vis[e.v]) dfs(e.v); } }}solver;int n, m, x, S, T, ans;LL sum, flow;void solve(){ sum = 0; ans = 0; solver.init(n + 2); S = 0, T = n + 1; FOR(i, 1, n){ scanf("%d", &x); if(x > 0){ solver.add(S, i, x); sum += (LL)x; } else solver.add(i, T, -x); } REP(i, 0, m){ int u, v; scanf("%d%d", &u, &v); solver.add(u, v, INF); } flow = solver.maxflow(S, T); solver.dfs(S); FOR(i, 1, n) if(solver.vis[i]) ans++; printf("%d %I64d\n", ans, sum - flow);}int main(){// freopen("in.txt", "r", stdin); while(~scanf("%d%d", &n, &m)){ solve(); } return 0;}
0 0
- POJ 2987 Firing (最大权闭合子图Dinic)
- 【POJ】2987 Firing 最大权闭合子图
- POJ 2987 Firing 最大权闭合子图
- poj 2987 Firing 最大权闭合子图
- poj 2987 Firing (最大权闭合子图)
- POJ 2987 Firing 最大权闭合子图
- poj 2987 Firing 最大权闭合子图
- POJ 2987 Firing 最小割(最大权闭合子图)
- POJ 2987 Firing(最大权闭合子图)
- poj 2987 Firing (最大权闭合子图)
- poj 2987 Firing 最大权闭合子图
- 最大权闭合子图(poj 2987 Firing)
- poj 2987 Firing ------最大权闭合图
- poj 2987 Firing(最大权闭合图)
- POJ 2987 Firing (最大权闭合图)
- POJ 2987 Firing 最大权闭合图
- poj 2987 Firing 最大权闭合图
- POJ 2987 Firing 最大权闭合图
- 51testing自动化测试学习第二天
- 台大机器学习听课笔记---RF 10-3 Feature Selection
- 51testing自动化测试学习第三天
- mysql 性能优化方案
- 第13周-多态性-项目1-动物这样叫
- POJ 2987 Firing 最大权闭合子图
- zabbix无法登录,session未记录
- 监控MSSQL(SQL Server)
- IIS7并发数优化
- Linux netstat命令详解
- 【转载】Linux/Unix笔记本
- 实例讲解虚拟机3种网络模式(桥接、…
- 自定义view的简单使用
- 用Hadoop进行分布式并行编程