Destroying the Graph
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 7597 |
|
Accepted: 2434 |
|
Special Judge |
Description
Alice and Bob play the following game. First, Alice draws some directed graph with N vertices and M arcs. After the Bob tries to destroy it. In a move he could take any vertex of the graph and remove either all arcs incoming to this vertex, or all arcs outgoing F Rom this vertex.
Alice assigns the 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 pay S wi-dollars.
Find out how 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 <=, 1 <= M <= 5000). The second line contains N integer numbers specifying wi+. The third line defines wi-in a similar. All costs is positive and does not exceed 106. Each of the following M lines contains and 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 has to remove all arcs from the graph. On the second line print K---the number of moves Bob needs to do it. After the print K lines that describe Bob ' s moves. Each line must first contain the number of the vertex and so ' + ' or '-' character, separated by one space. Character ' + ' means that Bob removes all arcs incoming to the specified vertex and '-' That's Bob removes all arcs Outgoin G from the specified vertex.
Sample Input
3 61 2 34 2 11 21 13 21 23 12 3
Sample Output
531 +2-2 +
Test Instructions: give you a map, for point I delete all the edges into the point to pay the cost w[i]+ (case 1), delete all the edge from this point to pay the cost w[i]-, ask to delete all the edges in the diagram at least how much cost (case 2).
Analysis: First we according to the test instructions, the point can delete some edges, then this can be seen as "with points to cover the Edge", here is nothing but the edge into 2 categories,
We can put the original point of the split, then completely equivalent to "use point to cover the edge", if the payment is 1, then this is the "minimum point coverage set" problem, but the cost is not certain, then this is the "minimum point weight coverage set" problem, with the help of binary matching ideas, we can introduce "minimum cut" to solve " Minimum point weight Override "issue.
Build diagram: The left lattice is the point of the case 2, the right lattice is the point of the situation 1, the right lattice with the meeting point t even flow is w+, the left lattice with the source point s even cost W,
For the input edge <u, the v> Edge (U, v+n) cost is infinity INF. Run one side of the maximum flow to find the minimum cost.
Output solution: Do not understand AH.
void solve (int u) { Mark[u] = 1; for (int i = head[u]; i =-1; i = edge[i].next) { int v = edge[i].v; if (Edge[i].cap-edge[i].flow > 0 &&!mark[v]) solve (v); }}
Summary: the two-match problem can be solved with the maximum flow, in the binary map has "minimum point coverage set" and "most dozen independent set", if there is a point of power, then the maximum flow (minimum cut) to solve the "minimum point weight coverage set" (Minimum cut) and "Maximum point right independent set" (maximum flow) problem.
The above analysis comes from http://blog.csdn.net/pi9nc/article/details/27112091
Look at other people's resolution one afternoon, for the smallest cut set of that piece still do not understand, other big God also is write with SAP ,SAP output solution set and Dinic output solution set code is not the same, and the minimum cut not understanding, only 1.1 points of debugging, tried an afternoon finally tried out. Drunk, the theory is not solid, will only use the template of the criticism. Struggle to understand the maximum flow and minimum cut.
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath > #define MAXN 5500#define maxm 1000000#define INF 0x3f3f3f3fusing namespace Std;int m, N;int HEAD[MAXN],CUR[MAXN], CNT; int DIST[MAXN], vis[maxn];int mark[maxn];struct node{int u, V, cap, flow, next;}; Node Edge[maxm];void init () {cnt = 0; Memset (Head,-1, sizeof (head)); memset (mark, 0, sizeof);} void Add (int u, int v, int w) {node E1 = {u, V, W, 0, Head[u]}; EDGE[CNT] = E1; Head[u] = cnt++; Node E2 = {V, u, 0, 0, Head[v]}; EDGE[CNT] = E2; HEAD[V] = cnt++;} void Getmap () {int A, u, v; for (int i = 1; I <= n; ++i) {scanf ("%d", &a); Add (i + N, n * 2 + 1, a); } for (int i = 1; I <= n; ++i) {scanf ("%d", &a); Add (0, I, a); } while (m--) {scanf ("%d%d", &u, &v); Add (u, v + N, INF); }}bool BFS (int st, int ed) {queue<int>q; memset (Vis, 0, sizeof (VIS));memset (Dist,-1, sizeof (Dist)); VIS[ST] = 1; DIST[ST] = 0; Q.push (ST); while (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i]; if (!VIS[E.V] && e.cap > E.flow) {vis[e.v] = 1; DIST[E.V] = Dist[u] + 1; if (e.v = = ed) return true; Q.push (E.V); }}} return false;} int DFS (int x, int ed, int a) {if (x = = Ed | | a = = 0) return A; int flow = 0, F; for (int &i = cur[x]; I! =-1; i = Edge[i].next) {node &e = Edge[i]; if (dist[e.v] = = Dist[x] + 1 && (f = DFS (e.v, ed, Min (A, e.cap-e.flow))) > 0) {e.flow + = f; edge[i ^ 1].flow-= f; A-= f; Flow + + F; if (a = = 0) break; }} return flow;} int Maxflow (int st, int ed) {int flowsum = 0; while (BFS (St, ed)) {memcpy (cur, head, sizeof (head)); Flowsum + = DFS (St, Ed, INF); } return flowsum;} void solve (int u) {mark[u] = 1; for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V; if (Edge[i].cap-edge[i].flow > 0 &&!mark[v]) solve (v); }}int Main () {while (scanf ("%d%d", &n, &m)! = EOF) {init (); Getmap (); printf ("%d\n", Maxflow (0, 2 * n + 1)); Solve (0); int ans= 0; for (int i = 1; I <= n; ++i) {if (!mark[i]) ans++; if (Mark[i + n]) ans++; } printf ("%d\n", ans); for (int i = 1;i <= n; ++i) {if (!mark[i]) printf ("%d-\n", i); if (Mark[i + n]) printf ("%d +\n", i); }} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2125--destroying the Graph "min cut solution" minimum point weight coverage problem "&& output solution (cut edge set) && all kinds of ignorance"