hdu 4738 Caocao's Bridges

Caocao's Bridges

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

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

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
 
Source
 
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6032 6031 6030 6029 6028 
 
题意:
曹操在长江上建了许多桥,连接了许多岛,周瑜要炸曹操的一座桥,每座桥都有一定数目的守卫,炸桥成功有以下两个条件:
1、炸桥人数>=桥上守卫人数
2、炸掉这座桥后,岛不能形成强联通(强联通:任意两岛都联通)
问最少要派出的炸桥人数,若不能完成,输出-1
 
题解:塔尖算法求桥,所有桥的最少守卫就是答案
注意:1、有重边
         2、如果最少守卫=0,那么要派出一个人
         3、图如果一开始就不连通,那么不用派人
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 1001
using namespace std;
int tot,front[N],to[N*N*2],nxt[N*N*2],val[N*N*2];
int dfn[N],low[N],id,ans;
bool v[N],ok;
int n,m;
void add(int u,int v,int w)
{
    to[++tot]=v; nxt[tot]=front[u]; front[u]=tot; val[tot]=w; 
    to[++tot]=u; nxt[tot]=front[v]; front[v]=tot; val[tot]=w;
}
void tarjer(int u,int pre)
{
    dfn[u]=low[u]=++id;
    for(int i=front[u];i;i=nxt[i])
    {
        if(i==(pre^1)) continue;
        if(!dfn[to[i]])
        {
            tarjer(to[i],i);
            low[u]=min(low[u],low[to[i]]);
            if(low[to[i]]>low[u]) ans=min(ans,val[i]);
        }
        else  low[u]=min(low[u],dfn[to[i]]);
    }
}
void pre()
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(front,0,sizeof(front));
    memset(v,0,sizeof(v));
    tot=1; id=0; ans=0x7fffffff;
    ok=false;
}
int main()
{
    int u,v,w;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(!n) return 0;
        pre();
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);
        }
        for(int i=1;i<=n;i++)
            if(!dfn[i]) 
            {
                if(i!=1) 
                {
                    printf("0\n");
                    ok=true;
                    break;
                }
                tarjer(1,0);    
            }
        if(ok) continue;
        if(ans==0x7fffffff) ans=-1;
        else if(ans==0) ans=1;
        printf("%d\n",ans);
    }
}

 

 
posted @ 2017-05-30 11:40  TRTTG  阅读(240)  评论(0编辑  收藏  举报