Caocao's Bridges-HDU4738(Tarjin+求桥)

http://acm.hdu.edu.cn/showproblem.php?pid=4738

题目大意:

给定n个点和m条边  和每条边的价值,求桥的最小价值(最小桥)

看着挺简单的但是有好多细节:

1、会有重边

2、如果最小价值是0的话应该输出1

3、m条边有可能不能连通n个点,这个时候没有花费。

Caocao‘s Bridges

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3108    Accepted Submission(s): 982

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>

using namespace std;
#define N 1010
#define INF 0x3f3f3f3f

struct node
{
    int to,flew,next;
}edge[N*N];

int low[N],dfn[N],Time,top,ans,Stack[N],belong[N],sum,head[N];

void Inn()
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(Stack,0,sizeof(Stack));
    memset(belong,0,sizeof(belong));
    memset(head,-1,sizeof(head));
    Time=top=ans=sum=0;
}

void add(int from,int to,int flew)
{
    edge[ans].to=to;
    edge[ans].flew=flew;
    edge[ans].next=head[from];
    head[from]=ans++;
}
void Tarjin(int u,int f)
{
    int v,k=0;
    low[u]=dfn[u]=++Time;
    Stack[top++]=u;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].to;
        if(v==f && !k)
        {
            k++;
            continue;
        }
        if(!dfn[v])
        {
            Tarjin(v,u);
            low[u]=min(low[u],low[v]);
        }
        else
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        sum++;
        do
        {
            v=Stack[--top];
            belong[v]=sum;
        }while(v!=u);
    }

}

void solve(int n)
{
    int k=0;
    int Min=INF;
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
        {
            k++;
            Tarjin(i,0);
        }
    }
    if(sum==1)
    {
        printf("-1\n");
        return;
    }
    if(k>1)
    {
        printf("0\n");
        return;
    }
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j!=-1;j=edge[j].next)
            {
                int u=belong[i];
                int v=belong[edge[j].to];
                if(u!=v)
                {
                    Min=min(Min,edge[j].flew);
                }
            }
        }
    if(Min==0)
        Min++;
        printf("%d\n",Min);
}
int main()
{
    int n,m,a,b,c,i;
    while(scanf("%d %d",&n,&m),n+m)
    {
        Inn();
        for(i=0;i<m;i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        solve(n);
    }
    return 0;
}

Caocao's Bridges-HDU4738(Tarjin+求桥)

时间: 2024-10-12 16:14:51

Caocao's Bridges-HDU4738(Tarjin+求桥)的相关文章

Caocao&#39;s Bridges HDU - 4738 找桥

题意: 曹操在赤壁之战中被诸葛亮和周瑜打败.但他不会放弃.曹操的军队还是不擅长打水仗,所以他想出了另一个主意.他在长江上建造了许多岛屿,在这些岛屿的基础上,曹操的军队可以轻易地攻击周瑜的军队.曹操还修建了连接岛屿的桥梁.如果所有的岛屿都用桥连接起来,曹操的军队就可以很方便地部署在这些岛屿之间.周瑜无法忍受,他想毁掉曹操的一些桥梁,把一个或多个岛屿与其他岛屿分开.周瑜身上只有一颗炸弹,是诸葛亮留下的,所以他只能毁掉一座桥.周瑜必须派人带着炸弹去炸毁那座桥.桥上可能有警卫.轰炸队的士兵人数不能少于一

【模板】【hdu4738】Caocao&#39;s Bridges——tarjan求桥

题目链接 题目大意: 曹操有N个岛,这些岛用M座桥连接起来,每座桥有士兵把守(也可能没有), 诸葛亮把所有炸弹都带走了,只留下一枚给周瑜(真狠). 周瑜想让这N个岛不连通,但需要派出不小于守桥士兵数的人去炸桥,因为只有一枚炸弹,因此只够炸掉一座桥. 分析: 很明显的求代价最小的桥,当然这道题有几个特殊的地方: 1.图本来就不联通,输出0: 2.无解(不存在桥),输出-1: 3.没人把守,但你还是得派一个人炸桥,输出1: 4.一般情况,输出最小代价. 剩下的就是模板了,不过需要注意的一点是,这道题

HDU4738 Caocao&#39;s Bridges 无向图的桥

一眼题:找所有的桥,然后求最小权值 但是有很多坑点 1:如果本来不联通 输出0,(这个坑我知道) 2:但是还有一个坑,就是当整个连通,最小桥的权值是0时,也必须派一个人去,wa了无数遍(还是太年轻) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <string> #include

HDU4738 Caocao&#39;s Bridges(桥)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 题意:给定一张无向图,求其中权值最小的一座桥,派最少的士兵去炸掉它!! 思路:直接用tarjan计算出桥并且取其中权值最小者. 此题坑点甚多,1.有可能桥本来就不联通,输出-1.2.桥最小者为0,输出1(至少排一个人去炸桥).3.不要去重边,两个岛之间允许有多座桥,tarjan忽略返回边只忽略一次,加pre_num处理下. 代码: #include<iostream> #include<cstd

HDU 4738 --Caocao&#39;s Bridges 【无向图边双联通 &amp;&amp; 求权值最小的桥 &amp;&amp; 模板】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2641    Accepted Submission(s): 855 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges 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 Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

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

HDU 4738 Caocao&#39;s Bridges ——(找桥,求联通块)

题意:给你一个无向图,给你一个炸弹去炸掉一条边,使得整个图不再联通,你需要派人去安置炸弹,且派去的人至少要比这条边上的人多.问至少要派去多少个,如果没法完成,就输出-1. 分析:如果这个图是已经是多个联通块了,那么一个人都不用去,如果不是,那么只要找出这个无向图上的桥并且哨兵数量最少的那座把它炸了就行(输出这条边上的哨兵数量即可).直接tarjan就可以写. 注意点:1.可能有重边,所以用手写邻接表的方式存图:2.如果一座桥上没有哨兵,那么你也得至少派去一个人去安置炸弹(因为炸弹不会自己飞过去啊