hdu 4738 Caocao's Bridges 【求最小代价的割边(桥)】

来源:互联网 发布:asp 网页访问数据库 编辑:程序博客网 时间:2024/05/22 06:14
                            Caocao's Bridges            Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn’t give up. Caocao’s army still was 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 troop. 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 be seperated from other islands. But Zhou Yu had only one bomb which was 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 be guards on bridges. The soldier number of the bombing team couldn’t be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.

Input
There are no more than 12 test cases.
In each test case:
The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N2 )
Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )
The input ends with N = 0 and M = 0.

Output
For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn’t succeed any way, print -1 instead.

Sample Input
3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0

Sample Output
-1
4

题目大意:曹操在赤壁之战中失利,但他并没有放弃,他想到一个好主意:填江造陆。在长江上建造人工陆地,并且陆地之间建桥方便到达。这时候,周瑜看不下去了,他决定炸桥,使得不同陆地之间分离,但只有一个炸弹,他想以最小的死伤数完成炸桥任务。请你帮他计算。。。。

知识点:割边(桥)

AC代码:

# include <cstdio># include <cstring># include <algorithm>using namespace std;# define MAXN 1005# define INF 1000000000struct EDGE{    int v;    int cut;    int w;    int next;}edge[MAXN * MAXN];int tot;int top;int scc;int index;int bridge;int head[MAXN];int dfn[MAXN];int low[MAXN];int belong[MAXN];int stack[MAXN];int instack[MAXN];int minsoldiers;void Init(){    tot = 0;    top = 0;    scc = 0;    index = 0;    minsoldiers = INF;    memset(head, -1, sizeof(head));    memset(dfn, 0, sizeof(dfn));    memset(instack, 0, sizeof(instack));}void Addedge(int u, int v, int w){    edge[tot].v = v;    edge[tot].cut = 0;    edge[tot].w = w;    edge[tot].next = head[u];    head[u] = tot++;}void Tarjan(int u, int pre){    int v;    dfn[u] = low[u] = ++index;    int flag = 1;    for (int i = head[u]; i != -1; i = edge[i].next)    {        v = edge[i].v;        if (flag && v == pre) //去重        {            flag = 0;            continue;        }        if (!dfn[v])        {            Tarjan(v, u);            if (low[v] < low[u])            {                low[u] = low[v];            }            if (low[v] > dfn[u])            {                bridge++;                edge[i].cut = 1;                if (edge[i].w < minsoldiers)                {                    minsoldiers = edge[i].w;                }            }        }        else if (dfn[v] < low[u])        {            low[u] = dfn[v];        }    }}void Solve(int n){    int i;    int mark = 1;    Tarjan(1, 1);    for (i = 1; i <= n; i++)    {        if (!dfn[i])        {            mark = 0;                   }    }    if (!mark) //当一开始就有孤立的陆地时    {        printf("0\n");        return;    }    if (INF == minsoldiers) //当所建的桥没有割边时    {        printf("-1\n");        return;    }    if (!minsoldiers) //当是割边,并且该桥上没有守卫时    {        printf("1\n");        return ;    }    if (minsoldiers) //有损伤,但能完成任务时    {        printf("%d\n", minsoldiers);        return ;    }}int main(void){    int n, m;    while (~scanf("%d %d", &n, &m), n && m)    {        int i;        Init();        for (i = 1; i <= m; i++)        {            int u, v, w;            scanf("%d %d %d", &u, &v, &w);            Addedge(u, v, w);            Addedge(v, u, w);        }        Solve(n);    }    return 0;}
0 0
原创粉丝点击