程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU 4738 Caocao's Bridges(求價值最小的橋)

HDU 4738 Caocao's Bridges(求價值最小的橋)

編輯:關於C++
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 2013 ACM/ICPC Asia Regional Hangzhou Online 求價值最小的橋: 注意重邊。如果不聯通輸出0,如果最小橋為0輸出1,如果無橋輸出-1.
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pairpil;
const int maxn=1000+10;
const int maxm=maxn*maxn;
struct node{
    int to,next;
    int val;
    bool col;//為橋
}e[maxm];
int head[maxn],cnt,cntE;
int DFN[maxn],low[maxn];
int s[maxn],instack[maxn];
int idex,top,bridge;
int belong[maxn],f[maxn];
int col[maxn],add[maxn];
int n,m;
void init()
{
    cnt=top=idex=bridge=cntE=0;
    CLEAR(head,-1);
    CLEAR(DFN,0);
    CLEAR(low,0);
    CLEAR(instack,0);
    CLEAR(belong,0);
    CLEAR(f,-1);
}
void addedge(int u,int v,int w)
{
    e[cntE].to=v;e[cntE].next=head[u];
    e[cntE].val=w;
    e[cntE].col=false;head[u]=cntE++;
}
void Tarjan(int u,int pre)
{
    low[u]=DFN[u]=++idex;
    s[top++]=u;
    instack[u]=1;
    int son=0;
    int pre_num=0;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(v==pre&&!pre_num)
        {
            pre_num++;
            continue;
        }
        if(!DFN[v])
        {
            son++;
            Tarjan(v,u);
            if(low[u]>low[v]) low[u]=low[v];
            if(low[v]>DFN[u])//橋
            {
                bridge++;
                e[i].col=true;
                e[i^1].col=true;
            }
            if(u!=pre&&low[v]>=DFN[u])//割點判斷1
            {
                col[u]=1;
                add[u]++;
            }
        }
        else if(low[u]>DFN[v])
            low[u]=DFN[v];
    }
    if(u==pre&&son>1)  col[u]=1;//割點判斷2
    if(u==pre)  add[u]=son-1;
    instack[u]=0;
    top--;
}
void work()
{
    REPF(i,1,n)
       if(!DFN[i])  Tarjan(i,i);
    int minn=0x3f3f3f3f;
    REPF(u,1,n)
    {
        for(int i=head[u];i!=-1;i=e[i].next)
            if(e[i].col&&minn>e[i].val)  minn=e[i].val;
    }
    if(minn==0)  puts("1");
    else if(minn==0x3f3f3f3f)  puts("-1");
    else  printf("%d\n",minn);

}
int find(int x)
{
    if(f[x]==-1)  return x;
    else  return f[x]=find(f[x]);
}
void Union(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x!=y)  f[x]=y;
}
int main()
{
    int u,v,w;
    while(~scanf("%d%d",&n,&m)&&(n+m))
    {
        init();
        REPF(i,1,m)
        {
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
            Union(u,v);
        }
        int flag=1;
        REPF(i,1,n)
            if(find(i)!=find(1))  flag=0;
        if(!flag)
        {
            puts("0");
            continue;
        }
        work();
    }
    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved