HDU-4738Caocao's Bridges

来源:互联网 发布:网络预约汽车运输证 编辑:程序博客网 时间:2024/06/05 02:52
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 <= N 2

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.


分析:只要使至少两个岛不连通就可以了,那么简单的求整个连通图的中桥然后求最小的权值就可以了。

就可以了吗?.这么简单了吗?.不不不.图论题如果很好想很好写的话要仔细想想题目里面有什么坑

首先是图论中都会遇到的重边问题,在这个题目中倘若某条边重复出现了,则这条边必然不可能是桥(仔细想想)

然后是这个题目中的坑点,首先原图不连通答案为零这个还蛮容易想到,然后倘若边上的最小值为零会怎么样?.答案

要为1,因为要有人扛炸药包......这个感觉是读题时的对题目的一种理解了,毕竟炸桥。

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<set>#include<vector>#include<iomanip>#include<queue>#include<map>#include<stack>using namespace std;typedef long long LL;typedef unsigned long long ULL;const int INF=0x3f3f3f3f,maxn=1010;int First[maxn],N,M,ans,cnt,dfn[maxn],low[maxn],dfs_clock;int vis[maxn][maxn],cnt1[maxn][maxn];struct node{    int to,Next,c;    bool f;}Tree[maxn*maxn*2];void add(int from,int to,int c){    Tree[cnt].to=to;    Tree[cnt].Next=First[from];    Tree[cnt].f=0;    Tree[cnt].c=c;    First[from]=cnt++;}void init(){    memset(cnt1,0,sizeof(cnt1));    memset(vis,INF,sizeof(vis));    memset(First,-1,sizeof(First));    cnt=dfs_clock=0;    ans=INF;    memset(dfn,0,sizeof(dfn));}void dfs(int u,int fa){    dfn[u]=low[u]=++dfs_clock;    for(int i=First[u];i!=-1;i=Tree[i].Next)    {        int v=Tree[i].to;        if(!dfn[v])        {            dfs(v,u);            low[u]=min(low[u],low[v]);            if(low[v]>dfn[u])            {                Tree[i].f=1;                Tree[i^1].f=1;            }        }        else if(v!=fa)            low[u]=min(low[u],dfn[v]);    }}int main(){   while(scanf("%d%d",&N,&M))   {       init();       if(N+M==0)        break;       for(int i=0;i<M;i++)       {           int a,b,d;           scanf("%d%d%d",&a,&b,&d);           cnt1[a][b]++;           cnt1[b][a]++;           if(d>vis[a][b])            continue;           vis[a][b]=vis[b][a]=d;           add(a,b,d);           add(b,a,d);       }       dfs(1,1);       for(int i=1;i<=N;i++)          if(!dfn[i])            ans=0;        if(ans==0)        {            printf("%d\n",ans);            continue;        }       for(int u=1;u<=N;u++)       {           for(int i=First[u];i!=-1;i=Tree[i].Next)           {               int v=Tree[i].to;               if(Tree[i].f&&cnt1[u][v]<2)                ans=min(ans,Tree[i].c);           }       }       if(ans==INF)        printf("%d\n",-1);       else        printf("%d\n",ans==0?1:ans);   }   return 0;}


原创粉丝点击