九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711577
题意:给定n个点,m条无向边
下面m行表示u , v ,边权值
求所有桥中最小的桥的权值,如不存在输出-1
若图一开始就不连通或最小权值为0则输出1
双连通求桥裸题
附赠一大波测试数据:
#include
#include
#include
#define N 1010
#define inf 10000000
using namespace std;
inline int Min(int a,int b){return a>b?b:a;}
vectorG[N];
int dis[N][N],bian[N][N];
int n,m,f[N];
int find(int x){return x==f[x]?x:f[x]=find(f[x]);}
void Union(int u,int v){
int fa=find(u),fb=find(v);
f[fa]=fb;
}
struct node{
int u,v,d;
}edge[N];//割边不会超过n条
int edgenum;
void PUT(int u,int v,int d){
node E={u,v,d};
edge[edgenum++]=E;
}
int pre[N],low[N],dfs_clock;
bool su;
int dfs(int u,int fa){//是连通图,dfs(u, )目的是寻找u的后代所能连回的(最早的祖先)的pre值
if(su)return 0;
int lowu=pre[u]= ++ dfs_clock;
int child=0;
for(int i=0;i pre[u]&&bian[u][v]==1){PUT(u,v,dis[u][v]);if(dis[u][v]<=1)return 0;}
}
else if(pre[v] < pre[u] && v!=fa)
lowu = Min(lowu, pre[v]);
}
return low[u]=lowu;
}
void findcut(){
memset(pre,0,sizeof(pre));
dfs_clock=0;
dfs(1,-1);//dfs(树根,树根的父亲是-1)
}
int main(){
int i,u,v,d;
while(scanf("%d %d",&n,&m),n)
{
memset(bian,0,sizeof(bian));
for(i=1;i<=n;i++)
{
G[i].clear();
f[i]=i;
for(int j=1;j<=n;j++)
dis[i][j]=inf;
}
while(m--)
{
scanf("%d %d %d",&u,&v,&d);
if(dis[u][v]==inf)
{
G[u].push_back(v); G[v].push_back(u);
Union(u,v);
}
dis[u][v]=dis[v][u]=Min(dis[u][v],d);
bian[u][v]++; bian[v][u]++;
}
bool fa=false; find(1);
for(i=1;i<=n;i++)if(find(i)!=f[1]){fa=true;break;}
if(fa){printf("0\n");continue;}
su=false;
edgenum=0;
findcut();
if(edgenum)
{
int ans=inf;
for(i=0;i
代码2:
#include
#include
#include
#include
#include
#include