hdu 4738
桥的应用!
虽然以前做过强联通分量的题,但刷的很水,所以比赛的时候一直想不起来是桥的应用;
反省一下~~~学习一下!
思路,找到权值最小的桥;用tarjin算法!
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #define maxn 1003 5 using namespace std; 6 7 struct edge 8 { 9 int st,ed,w,next; 10 }e[maxn*maxn*2]; 11 12 int head[maxn],dfn[maxn],low[maxn],nncount,best,num,dan; 13 14 void addedge(int x,int y,int w) 15 { 16 e[num].st=x,e[num].ed=y,e[num].w=w,e[num].next=head[x],head[x]=num++; 17 e[num].st=y,e[num].ed=x,e[num].w=w,e[num].next=head[y],head[y]=num++; 18 } 19 20 void tarjin(int x,int id) 21 { 22 int v; 23 dfn[x]=low[x]=nncount++; 24 for(int i=head[x]; i!=-1; i=e[i].next) 25 { 26 int v=e[i].ed; 27 if(i==(id^1)) continue; 28 if(dfn[v]==-1) 29 { 30 tarjin(v,i); 31 low[x]=min(low[v],low[x]); 32 if(low[v]>dfn[x]) 33 if(best>e[i].w) 34 best=e[i].w; 35 } 36 else low[x]=min(dfn[v],low[x]); 37 } 38 dan++; 39 } 40 41 int main() 42 { 43 int n,m,a,b,w; 44 while(scanf("%d%d",&n,&m)&&(n+m)) 45 { 46 memset(dfn,-1,sizeof dfn); 47 num=0,nncount=0,dan=0; 48 best=10000000; 49 memset(head,-1,sizeof head); 50 for(int i=0; i<m; i++) 51 { 52 scanf("%d%d%d",&a,&b,&w); 53 addedge(a,b,w); 54 } 55 tarjin(1,-1); 56 if(dan<n)puts("0"); 57 else if(best==10000000) puts("-1"); 58 else if(best==0) puts("1"); 59 else printf("%d\n",best); 60 } 61 return 0; 62 }