Hdoj 2435 There is a war "to find the smallest cut has been divided into two points set + enumeration two point set inside the point of the new edge to seek residual network maximum minimum cut"

Source: Internet
Author: User



There is a warTime limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 993 Accepted Submission (s): 283


Problem Description there is a sea.
There is N islands in the sea.
There is some directional bridges connecting these islands.
There is a country called country one located on Island 1.
There is another country called country another located on island N.
There is a war against country another, which launched by country one.
There is a strategy which can help country another to defend this war by destroying the bridges for the purpose of making Island 1 and Island n disconnected.
There is some different destroying costs of the bridges.
There is a prophet in country another who's clever enough to find the minimum total destroying costs to achieve the Strat Egy.
There is an architecture in country one who's capable enough to rebuild a bridge to make it unbeatable or build a new inv Incible directional bridge between any and countries from the subset of the island 2 to island n-1.
There is isn't enough time for country one, so it can only build one new bridge, or rebuild one existing bridge before the C Ountry another starts destroying, or do nothing if happy.
There is a problem:country one wants to maximize the minimum total destroying costs country another needed to achieve the Strategy by making, the best choice. Then what ' s the maximum possible result?

Input There is multiple cases in this problem.
There is a line with an integer telling your the number of cases at the beginning.
The is both numbers in the first line of every case, N (4<=n<=100) and M (0<=m<=n* (n-1)/2), indicating the Numbe R of Islands and the number of bridges.
There is M lines following, each one of the which contains three integers a, B and C, with 1<=a, B<=n and 1<=c<=1 0000, meaning that there was a directional bridge from A to B with C being the destroying cost.
There is no lines containing the same A and B.

Output there is a line with one integer for each test case, telling the maximun possible result.

Sample Input
44 04 21 2 23 4 24 31 2 12 3 13 4 104 31 2 52 3 23 4 3

Sample Output
0213

Test instructions: n m The edge of a connected island. Known on the island 1 There is a city a , on the island n There is a city b a to attack the city b , City b a to its own path.

City b a to b path.

City a {2 - n-1} Choose two points inside a " b Create a new a to b m b pay the most price, They will definitely choose the optimal building plan.

Ask you: City B needs to pay the greatest price.

Analysis: The minimum cut will certainly divide the original image into two point sets. One is the set of S that can be reached by the source point , and one is the T -set that can reach the meeting point (reachable is for the residual network). Point a belongs to the S set, and points b belongs to T Set, So the optimal construction plan must be built on the side of a-B.

Implementation process

1, find the smallest cut ans of the original image;

2, the S set and T set are obtained in the residual network ;

3, enumerate two the points inside the collection, and build a new edge in the residual network to seek the smallest cut. Get the maximum minimum cut need;

4, the final result is ans+need.


AC Code:


#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 110# Define MAXM 20000+10#define INF 0x3f3f3f3fusing namespace std;struct edge{int from, to, cap, flow, next;}; Edge EDGE[MAXM], Redge[maxm];int HEAD[MAXN], RHEAD[MAXN], Edgenum, Redgenum, Cur[maxn];int Dist[maxn];bool Vis[MAXN];    int N, m;void init () {edgenum = 0; Memset (Head,-1, sizeof (head));}    void Addedge (int u, int v, int w) {Edge E1 = {u, V, W, 0, Head[u]};    Edge[edgenum] = E1;    Head[u] = edgenum++;    Edge E2 = {V, u, 0, 0, Head[v]};    Edge[edgenum] = E2; HEAD[V] = edgenum++;}    BOOL BFS (int s, int t) {queue<int> Q;    memset (Dist,-1, sizeof (Dist));    Memset (Vis, false, sizeof (VIS));    Dist[s] = 0;    Vis[s] = true;    Q.push (s); while (!        Q.empty ()) {int u = q.front ();        Q.pop ();            for (int i = head[u]; i =-1; i = Edge[i].next) {Edge E = Edge[i];      if (!vis[e.to] && e.cap > E.flow)      {Dist[e.to] = Dist[u] + 1;                if (e.to = = t) return true;                Vis[e.to] = true;            Q.push (e.to); }}} return false;}    int DFS (int x, int a, int t) {if (x = = T | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; i =-1; i = Edge[i].next) {Edge &e = Edge[i]; if (dist[e.to] = = Dist[x] + 1 && (f = DFS (e.to, Min (A, e.cap-e.flow), T)) > 0) {Edge[i].flo            W + = f;            Edge[i^1].flow-= f;            Flow + + F;            A-= f;        if (a = = 0) break; }} return flow;}    int Maxflow (int s, int t) {int flow = 0;        while (BFS (s, t)) {memcpy (cur, head, sizeof (head));    Flow + = DFS (s, INF, T); } return flow;}        int SR[MAXN], tr[maxn];//record s set T set inside dot int s, t;void find_s (int u) {for (int i = head[u]; I! =-1; i = Edge[i].next) {        Edge E = Edge[i];        if (vis[e.to]) continue; if (E.cap > E. Flow) {Vis[e.to] = true;            sr[s++] = e.to;        find_s (e.to);    }}}void Solve () {init ();    int A, b, C;        for (int i = 0; i < M; i++) {scanf ("%d%d%d", &a, &b, &c);    Addedge (A, B, c);    } int ans = maxflow (1, N);//Minimum cut memset (Vis, false, sizeof (VIS));    S = 0;    VIS[1] = TRUE;//1 can no longer access find_s (1);    T = 0;    for (int i = 2; I <= N-1; i++) if (!vis[i]) tr[t++] = i;//records the points inside the T-set memcpy (Rhead, head, sizeof (head));    memcpy (Redge, Edge, sizeof (edge));    Redgenum = Edgenum; int need = 0;//extra cost for (int i = 0; i < S; i++) {for (int j = 0; J < T; J + +) {memcpy (            Head, Rhead, sizeof (Rhead));            memcpy (Edge, Redge, sizeof (Redge));            Edgenum = Redgenum;        Addedge (Sr[i], tr[j], INF);//Jianxin side need = max (need, Maxflow (1, N)); }} printf ("%d\n", Ans+need);}    int main () {int t;    scanf ("%d", &t);    while (t--){scanf ("%d%d", &n, &m);    Solve (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdoj 2435 There is a war "to find the smallest cut has been divided into two points set + enumeration two point set inside the point of the new edge to seek residual network maximum minimum cut"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.