您的位置:首页 > 其它

HDU 4738 Caocao's Bridges 桥+并查集

2015-05-16 20:20 295 查看

Caocao's Bridges

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1976 Accepted Submission(s): 701



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0


[align=left]Sample Output[/align]

-1
4


[align=left]Source[/align]
2013 ACM/ICPC Asia Regional Hangzhou Online

/** Author: ☆·aosaki(*’(OO)’*)  niconiconi★ **/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include<bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll long long
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;
inline int gcd(int a,int b)
{
return a==0?b:gcd(b%a,a);
}

#define INF 1e8
#define eps 1e-8
#define mod 10007
#define maxn 1011
#define maxm 2000010

using namespace std;

int col[maxn];
int dfn[maxn],low[maxn],stack[maxn];
int pre[maxn];
int n,m,tot=0,cot,tm=0,index=0,top=0;
int pa[maxn];
bool instack[maxn];
bool cut[maxn];
int add_block[maxn];
int bridge;

void ms()
{
lp(i,n) pa[i]=i;
}

struct Side
{
int to,next;
int w;
bool cut;
}e[maxm];

int fin(int x)
{
return (x==pa[x])?x:pa[x]=fin(pa[x]);
}

void uni(int x,int y)
{
x=fin(x); y=fin(y);
if(x==y) return;
pa[y]=x;
}

void add(int u,int v,int w)
{
e[tot].to=v;
e[tot].next=pre[u];
e[tot].w=w;
e[tot].cut=0;
pre[u]=tot++;
}

void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++index;
stack[top++]=u;
instack[u]=true;
int son=0;
int pre_num=0;
for(int i=pre[u];i!=-1;i=e[i].next)
{
v=e[i].to;
if(v==fa && pre_num==0)
{
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].cut=true;
e[i^1].cut=true;
}
if(u!=fa && low[v]>=dfn[u])
{
cut[u]=true;
add_block[u]++;
}
}
else if(low[u]>dfn[v])
low[u]=dfn[v];
}
if(u==fa && son>1) cut[u]=true;
if(u==fa) add_block[u]=son-1;
instack[u]=false;
top--;
}

void init()
{
mem(col);
mem1(pre);
mem(dfn);
mem(low);
mem(stack);
mem(instack);
mem(cut);
mem(add_block);
tot=0;
top=0;
tm=0;
index=0;
cot=0;
bridge=0;
}

int main()
{
//freopen("in.txt","r",stdin);
int uu,vv,ww;
while(~sc2(n,m) && n!=0)
{
ms();
init();
lp(i,m)
{
sc2(uu,vv);
sc(ww);
add(uu,vv,ww);
add(vv,uu,ww);
uni(uu,vv);

}
int f=0;
lp(i,n)
{
if(fin(i)!=fin(1))
{
f=1;
break;
}
}
if(f)
{
printf("0\n");
continue;
}
lp(i,n)
if(!dfn[i])
{
tarjan(i,i);
}
int re=INF;
for(int u=1;u<=n;u++)
for(int i=pre[u];i!=-1;i=e[i].next)
if(e[i].cut)
re=min(re,e[i].w);
if(re==0)
{
printf("1\n");
continue;
}
if(re==INF)
{
printf("-1\n");
continue;
}
printf("%d\n",re);

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: