hdu 4738(雙聯通縮點)


此題有坑!!!

橋的邊權有可能是0,但是這時候要輸出1,因為周瑜至少的排一個人去炸呀,那個年代又沒有導彈!!!卧槽,有時候還得聯系實際!

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<vector>
using namespace std;
const int inf = 1<<20;
const int maxn = 1005;
int n,m;
struct node
{
int to;
int next;
int w;
node(){}
node(int a,int b,int c):to(a),next(b),w(c){}
}edge[1002*1002];
struct Edge
{
int to;
int w;
Edge(int a,int b):to(a),w(b){}
};
vector<Edge>G[maxn];
int head[maxn],dfn[maxn],low[maxn],belong[maxn];
int tot,ti,cnt;
stack<int>s;
void tarjan(int u,int fa)
{
dfn[u] = ++ti; low[u] = ti;
s.push(u); int flag = 0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to;
if(v==fa&&flag==0){flag=1;continue;}
if(!dfn[v])
{
tarjan(v,u);
low[u] = min(low[u],low[v]);
}
else if(belong[v]==-1)
{
low[u] = min(dfn[v],low[u]);
}
}
if(dfn[u]==low[u])
{
int y; cnt++;
do{
y = s.top(); s.pop();
belong[y] = cnt;
}while(y!=u);
}
}
int res;
void dfs(int u,int fa)
{
int len = G[u].size();
for(int i=0;i<len;i++)
{
int v = G[u][i].to;
if(v==fa) continue;
res = min(res,G[u][i].w);
dfs(v,u);
}
}
void cbh(int u)
{
dfn[u] = 1;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to;
if(!dfn[v])
{
dfn[v] = 1;
cbh(v);
}
}
}
int main()
{
int u,v,w;
while(scanf("%d%d",&n,&m))
{
if(n==0&&m==0) break;
tot = 0;
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(u==v) continue;
edge[tot] = node(v,head[u],w);
head[u] = tot++;
edge[tot] = node(u,head[v],w);
head[v] = tot++;
}
int key = 0;
memset(dfn,0,sizeof(dfn));
for(int i=1;i<=n;i++)
{
if(!dfn[i]){key++;cbh(i);}
}
if(key>1){printf("0\n");continue;}
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(belong,-1,sizeof(belong));
ti = 0; cnt = 0;
for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i,-1);
if(cnt==1) {printf("-1\n"); continue; }
for(int i=1;i<=n;i++) G[i].clear();
for(int i=1;i<=n;i++)
{
for(int j=head[i];j!=-1;j=edge[j].next)
{
if(belong[i]!=belong[edge[j].to])
{
G[belong[i]].push_back(Edge(belong[edge[j].to],edge[j].w));
}
}
}
res = inf;
dfs(1,-1);
if(res==0) res++;
printf("%d\n",res);
}
return 0;
}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com