Destroying The Graph
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.
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 +
Source
Northeastern Europe 2003, Northern Subregion
题目类型:最小点权覆盖(最小割)
算法分析:每个点都有两个属性(删除入边的权值和删除出边的权值),则拆点一个处理入边,一个处理出边,使得原图转化为二分图。对于给出的边<u, v>,从u向n+v连一条容量为INF的有向边,从源点向点1~n分别连一条容量为对应点出边点权的有向边,从点n~2n分别向汇点连一条容量为对应点入边点权的有向边。最后跑一个最大流就可得到最小费用值。最后从源点s开始dfs将在残余网络中在同一个连通分量中的点打上标记,所有1~n中没有标记的点就是选择删除出边的,n~2n中被标记的点就是选择删除入边的
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
/************************************************* Author :supermaker Created Time :2016/4/7 21:19:29 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 200 + 66; struct Node { int v, nxt, c, f; }; Node edge[maxn*maxn]; int head[maxn], edgelen; int dep[maxn], cnt[maxn], cur[maxn]; void Init () { memset (head, -1, sizeof (head)); memset (edge, -1, sizeof (edge)); edgelen = 0; } void AddEdge (int u, int v, int w, int rw = 0) { edge[edgelen].v = v, edge[edgelen].c = w, edge[edgelen].f = 0; edge[edgelen].nxt = head[u], head[u] = edgelen++; edge[edgelen].v = u, edge[edgelen].c = rw, edge[edgelen].f = 0; edge[edgelen].nxt = head[v], head[v] = edgelen++; } void bfs (int s, int e) { memset (dep, -1, sizeof (dep)); memset (cnt, 0, sizeof (cnt)); dep[e] = 0; cnt[dep[e]]++; queue <int> qu; qu.push (e); while (!qu.empty ()) { int u = qu.front (); qu.pop (); for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].v; if (dep[v] != -1) continue; qu.push (v); dep[v] = dep[u] + 1; cnt[dep[v]]++; } } } int sta[maxn]; int MaxFlow (int s, int e, int n) { bfs (s, e); int res = 0, top = 0, u = s; memcpy (cur, head, sizeof (head)); while (dep[s] < n) { if (u == e) { int minval = INF, minval_pos = -1; for (int i = 0; i < top; i++) if (minval > edge[sta[i]].c - edge[sta[i]].f) { minval = edge[sta[i]].c - edge[sta[i]].f; minval_pos = i; } for (int i = 0; i < top; i++) { edge[sta[i]].f += minval; edge[sta[i]^1].f -= minval; } res += minval; top = minval_pos; u = edge[sta[top]^1].v; } else { bool is_have = false; for (int i = cur[u]; i != -1; i = edge[i].nxt) { int v = edge[i].v; if (edge[i].c - edge[i].f && dep[v] + 1 == dep[u]) { cur[u] = i; sta[top++] = cur[u]; u = v; is_have = true; break; } } if (is_have) continue; int minval = n; for (int i = head[u]; i != -1; i = edge[i].nxt) if (edge[i].c - edge[i].f && dep[edge[i].v] < minval) { minval = dep[edge[i].v]; cur[u] = i; } cnt[dep[u]]--; if (!cnt[dep[u]]) return res; dep[u] = minval + 1; cnt[dep[u]]++; if (u != s) u = edge[sta[--top]^1].v; } } return res; } int in[maxn], out[maxn]; bool vis[maxn]; void dfs (int u) { vis[u] = true; for (int i = head[u]; i != -1; i = edge[i].nxt) { int v = edge[i].v; if (edge[i].c - edge[i].f && !vis[v]) dfs (v); } } int main() { //CFF; //CPPFF; int n, m; while (scanf ("%d%d", &n, &m) != EOF) { for (int i = 1; i <= n; i++) scanf ("%d", &in[i]); for (int i = 1; i <= n; i++) scanf ("%d", &out[i]); Init (); for (int i = 1; i <= m; i++) { int u, v; scanf ("%d%d", &u, &v); AddEdge (u, n + v, INF); } for (int i = 1; i <= n; i++) AddEdge (0, i, out[i]); for (int i = 1; i <= n; i++) AddEdge (n + i, 2 * n + 1, in[i]); printf ("%d\n", MaxFlow (0, 2 * n + 1, 2 * n + 2)); memset (vis, false, sizeof (vis)); dfs (0); int tot = 0; for (int i = 1; i <= n; i++) { if (!vis[i]) tot++; if (vis[n+i]) tot++; } printf ("%d\n", tot); for (int i = 1; i <= n; i++) { if (!vis[i]) printf ("%d -\n", i); if (vis[n+i]) printf ("%d +\n", i); } } return 0; } |