poj 2125 Destroying The Graph 最小点权覆盖集+拆点+求割边
Destroying The Graph
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 7570 | Accepted: 2423 | Special Judge |
Description
Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After that Bob tries to destroy it. In a move he may take any vertex of the graph and remove either all arcs incoming into this vertex, or all arcs outgoing from this vertex.
Alice assigns two costs to each vertex: Wi+ and Wi-. If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.
Alice assigns two costs to each vertex: Wi+ and Wi-. If Bob removes all arcs incoming into the i-th vertex he pays Wi+ dollars to Alice, and if he removes outgoing arcs he pays Wi- dollars.
Find out what minimal sum Bob needs to remove all arcs from the graph.
Input
Input
file describes the graph Alice has drawn. The first line of the input
file contains N and M (1 <= N <= 100, 1 <= M <= 5000). The
second line contains N integer numbers specifying Wi+. The third line defines Wi- in a similar way. All costs are positive and do not exceed 106
. Each of the following M lines contains two integers describing the
corresponding arc of the graph. Graph may contain loops and parallel
arcs.
Output
On
the first line of the output file print W --- the minimal sum Bob must
have to remove all arcs from the graph. On the second line print K ---
the number of moves Bob needs to do it. After that print K lines that
describe Bob's moves. Each line must first contain the number of the
vertex and then '+' or '-' character, separated by one space. Character
'+' means that Bob removes all arcs incoming into the specified vertex
and '-' that Bob removes all arcs outgoing from the specified vertex.
Sample Input
3 6 1 2 3 4 2 1 1 2 1 1 3 2 1 2 3 1 2 3
Sample Output
5 3 1 + 2 - 2 +
题意:给出N个点,N条边。有两种操作。一种是选择某个点把这个点的入边全部删除。另一种是给出选择某个点把这个点的出边全部删除。
给出选择每个点的两种操作所需要的花费。求删除所有边所需要的最小花费。
思路:设删除入边为操作a,删除出边为操作b。对于每条边(u,v)来讲,必须至少选择a(v)或者b(u)来进行操作,才能删除这条边。
这就符合点覆盖集的模型。
对于每个点,可以拆分成两个点,一种对应于操作a(N+1,2*N),另一种对应于操作b(1,N)。
所以增加源点s(0)和汇点t(2*N+1)。对于对应于删除出边的操作b。从s到每个点连一条边,容量为对应花费。
对于删除入边的操作a,每个点到t连一条边 ,容量为对应花费。
然后图中给出点边(i, j),连接点(i,j+N),容量为inf.
然后求最小割,就是最小花费。
对于求割边。在最后一次bfs操作中已经标记了点s能够到达的点.
所以对于(1~N)的点。如果点s不能到达这个点,那么对应点边就是割边。这个点就是删除出边操作。
对于(N+1~2*N)的点。如果点s能够到达这个点,那么对应点就是割边。这个点就是删除入边操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include <iostream> #include <cstring> #include <cstdio> #include <string> #include <vector> #include <queue> using namespace std; int N, M; #define maxn 110 const int inf = 0x3f3f3f3f; struct Edge { int from, to, cap, flow; Edge( int f, int t, int c, int fl) { from = f; to = t; cap = c; flow = fl; } }; vector <Edge> edges; vector < int > G[maxn*2]; int vis[maxn*2], d[maxn*2], cur[maxn*2]; int n, m, s, t; 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() { memset (vis, 0, sizeof (vis)); vis[s] = 1; d[s] = 0; queue < int > q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for ( int i = 0; i < G[u].size(); i++) { Edge &e = edges[G[u][i]]; if (!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[u]+1; q.push(e.to); } } } return vis[t]; } int dfs( int x, int a) { if (x == t || a == 0) return a; int flow = 0, f; for ( int &i = cur[x]; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if (d[x]+1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i]^1].flow -= f; flow += f; a -= f; if (a == 0) break ; } } return flow; } int Maxflow() { int flow = 0; while (bfs()) { memset (cur, 0, sizeof (cur)); flow += dfs(s, inf); } return flow; } int win[maxn], wout[maxn]; int main() { while (~ scanf ( "%d%d" , &N, &M)) { edges.clear(); for ( int i = 0; i < maxn*2; i++) G[i].clear(); for ( int i = 1; i <= N; i++) scanf ( "%d" , &win[i]); for ( int i = 1; i <= N; i++) scanf ( "%d" , &wout[i]); s = 0; t = 2*N+1; n = N; for ( int i = 1; i <= N; i++) { AddEdge(s, i, wout[i]); AddEdge(i+N, t, win[i]); } for ( int i = 1; i <= M; i++) { int a, b; scanf ( "%d%d" , &a, &b); AddEdge(a, b+N, inf); } int ans = Maxflow(); printf ( "%d\n" , ans); int cnt = 0; for ( int i = 1; i <= 2*N; i++) { if (vis[i] && i > N) cnt++; if (!vis[i] && i <= N) cnt++; } printf ( "%d\n" , cnt); for ( int i = 1; i <= 2*N; i++) { if (vis[i] && i > N) printf ( "%d +\n" , i-N); if (!vis[i] && i <= N) printf ( "%d -\n" , i); } } return 0; } |
分类:
网络流
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥