HDU 4738--caocao ' s Bridges "non-lateral edge double unicom && min. bridge && template for weight value"

Source: Internet
Author: User

Caocao ' s BridgesTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2641 Accepted Submission (s): 855


Problem Descriptioncaocao was defeated by Zhuge Liang and Zhou Yu in the Battle of Chibi. But he wouldn ' t give up. Caocao ' s army still is not good at water battles, so he came up with another idea. He built many islands in the Changjiang River, and based on those islands, Caocao ' s Army could easily attack Zhou Yu ' s tro Op. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao's army could be deployed very conveniently among those islands. Zhou Yu couldn ' t stand with that, so he wanted to destroy some Caocao ' s bridges so one or more islands would is seperated From the other islands. But Zhou Yu had only one bomb which is left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might is guards on bridges. The soldier number of the bombing team couldn ' t is less than the guard number of a bridge, or the mission would fail. Least how many soldiers Zhou Yu has To sent to complete the island seperating mission. 
Inputthere is no more than test cases.

In each test case:

The first line contains-integers, n and m, meaning that there is N islands and M bridges. All the islands is numbered from 1 to N. (2 <= N <=, 0 < M <= N2)

Next m lines describes M bridges. Each line contains three integers u,v and W, meaning this there is a bridge connecting island U and Island V, and there ar E W guards on the bridge. (u≠v and 0 <= W <= 10,000)

The input ends with N = 0 and M = 0.
Outputfor each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn ' t succeed any, print-1 instead.
Sample Input
3 31 2 72 3 43 1 43 21 2 72 3 40 0

Sample Output
-14
Test instructions: There are n-Islands and M-Bridge, each bridge has a W soldiers guarding, now to send not less than the number of soldiers to defend the bridge to the bridge, can only blow a bridge, so that the N-island is not connected, ask at least how many people to send.
Analysis: Only need to use the Tarjan algorithm to find the lowest weight in the graph of the bridge on the line.

Attention:
A: If the diagram is not connected, do not send people to blow the bridge, direct output 0

Two: There may be heavy edges

Three: If there are no soldiers on the bridge, then at least one person will be sent to blow up the bridge.

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 1010# Define MAXM 1000100#define INF 0x3f3f3f3fusing namespace std;int N, m;int low[maxn];int dfn[maxn];int HEAD[MAXN], Cnt;int Dfs_clock;int mark;//Determine if the graph is connected to the struct node{int u, V, W, CNT, next;};    Node Edge[maxm];void init () {cnt = 0; Memset (Head,-1, sizeof (head));}    void Add (int u, int v, int w) {edge[cnt] = {u, V, W, 0, Head[u]};    Head[u] = cnt++;    EDGE[CNT] = {V, u, W, 0, Head[v]}; HEAD[V] = cnt++;}        void input () {while (m--) {int u, V, W;        scanf ("%d%d%d", &u, &v, &w);    Add (U, V, W);    }}void Tarjin (int u, int fa) {Low[u] = dfn[u] = ++dfs_clock;    int flag = 1;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;            if (flag && v = = FA) {//de-heavy side, heavy edge cannot be bridge flag = 0;        Continue            } if (!dfn[v]) {Tarjin (V, u);        Low[u] = min (Low[u], low[v]);    if (Dfn[u] < LOW[V])//is the bridge edge[i].cnt = edge[i ^ 1].cnt = 1;    } else Low[u] = min (Low[u], dfn[v]);    }}void find () {memset (DFN, 0, sizeof (DFN));    memset (Low, 0, sizeof (low));    Dfs_clock = 0;    Tarjin (1, 1);    Mark = 1;            for (int i = 1; I <= n; ++i) {if (!dfn[i]) {mark = 0;        return;    }}}void Solve () {if (!mark) printf ("0\n");        else{int ans = INF;        for (int i = 0; i < cnt; ++i) {if (edge[i].cnt) ans = min (ans, edge[i].w);        } if (ans = = INF) ans =-1;        if (ans = = 0) ans = 1;    printf ("%d\n", ans);        }}int Main () {while (scanf ("%d%d", &n, &m), n | | m) {init ();        Input ();        Find ();    Solve (); } return 0;}

Another way to go back to the edge of the problem, the processing time is relatively slow

#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #define MAXN 1010# Define MAXM 1000100#define INF 0x3f3f3f3fusing namespace std;int N, m;int low[maxn];int dfn[maxn];int HEAD[MAXN], Cnt;int    Dfs_clock;int mark;//Determine if the graph is connected to a struct node{int u, V, W, cnt, Next, again;//again tag is not a heavy edge};node edge[maxm];void init () {    CNT = 0; Memset (Head,-1, sizeof (head));}    void Add (int u, int v, int w) {int i;        Mark the heavy edge for (i = head[u]; I! = 1; i = edge[i].next) {if (edge[i].v = = v) {break;    }} if (i! =-1) edge[i].again = 1;        else{edge[cnt] = {u, V, W, 0, Head[u], 0};    Head[u] = cnt++;        }}void input () {while (m--) {int u, V, W;        scanf ("%d%d%d", &u, &v, &w);        Add (U, V, W);    Add (V, u, W);    }}void Tarjin (int u, int fa) {Low[u] = dfn[u] = ++dfs_clock;        for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;    if (v = = FA) continue;    if (!dfn[v]) {Tarjin (V, u);            Low[u] = min (Low[u], low[v]);        if (Dfn[u] < low[v] && Edge[i].again = = 0)//is the bridge edge[i].cnt = edge[i ^ 1].cnt = 1;    } else Low[u] = min (Low[u], dfn[v]);    }}void find () {memset (DFN, 0, sizeof (DFN));    memset (Low, 0, sizeof (low));    Dfs_clock = 0;    Tarjin (1,-1);    Mark = 1;            for (int i = 1; I <= n; ++i) {if (!dfn[i]) {mark = 0;        return;    }}}void Solve () {if (!mark) printf ("0\n");        else{int ans = INF;        for (int i = 0; i < cnt; ++i) {if (edge[i].cnt) ans = min (ans, edge[i].w);        } if (ans = = INF) ans =-1;        if (ans = = 0) ans = 1;    printf ("%d\n", ans);        }}int Main () {while (scanf ("%d%d", &n, &m), n | | m) {init ();        Input ();        Find ();    Solve (); } return 0;}


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

HDU 4738--caocao ' s Bridges "non-lateral edge double unicom && min. bridge && template for weight value"

Related Article

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.