hdu 4738 Caocao's Bridges (找桥,有重边)
2015-04-04 12:32
351 查看
题意:
给出一个图,现在只能切除一个路使得整个图不联,每条路都有保安(w个),要切除这条路需要至少需要人力w人。问图要使得整个图不连通需要的最少人力。
题解:
这题有一个坑点,就是如果路上没有保安的情况,这种情况也是需要有人去切除路的,所以至少1人。这题有重边。。。
给出一个图,现在只能切除一个路使得整个图不联,每条路都有保安(w个),要切除这条路需要至少需要人力w人。问图要使得整个图不连通需要的最少人力。
题解:
这题有一个坑点,就是如果路上没有保安的情况,这种情况也是需要有人去切除路的,所以至少1人。这题有重边。。。
#include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<vector> #include<queue> #include<map> #include<set> using namespace std; #define B(x) (1<<(x)) typedef long long ll; void cmax(int& a,int b){ if(b>a)a=b; } void cmin(int& a,int b){ if(b<a)a=b; } const int oo=0x3f3f3f3f; const int MOD=1000000007; const int maxn=1100; const int maxm=2100000; struct EDGE{ int v,next,w; }E[maxm]; int head[maxn],tol; int low[maxn],dfn[maxn]; int g_cnt; int ans; void Init(){ memset(head,-1,sizeof head); tol=0; memset(low,0,sizeof low); memset(dfn,0,sizeof dfn); g_cnt=0; ans=oo; } void add_edge(int u,int v,int w){ E[tol].w=w; E[tol].v=v; E[tol].next=head[u]; head[u]=tol++; } void Tarjan(int u,int pre){ dfn[u]=low[u]=++g_cnt; int v; for(int i=head[u];i!=-1;i=E[i].next){ if(i==(pre^1))continue; v=E[i].v; if(!dfn[v]){ Tarjan(v,i); if(low[v]<low[u]) low[u]=low[v]; if(low[v]>dfn[u]) cmin(ans,E[i].w); }else if(dfn[v]<low[u]) low[u]=dfn[v]; } } int main(){ //freopen("E:\\read.txt","r",stdin); int n,m,u,v,w; while(scanf("%d %d",&n,&m)!=EOF){ if(n==0&&m==0)break; Init(); for(int i=1;i<=m;i++){ scanf("%d %d %d",&u,&v,&w); add_edge(u,v,w); add_edge(v,u,w); } int cnt=0; for(int i=1;i<=n;i++) if(!dfn[i]){ cnt++; Tarjan(i,-1); } if(cnt>1) ans=0; else if(ans==oo) ans=-1; else if(ans==0) ans=1; printf("%d\n",ans); } return 0; } /* 3 3 1 2 7 2 3 4 3 1 4 3 2 1 2 7 2 3 0 0 0 */
相关文章推荐
- HDU 4738 Caocao's Bridges(找无向图的桥 双联通)
- HDU 4738 --Caocao's Bridges 【无向图边双联通 && 求权值最小的桥 && 模板】
- hdu 4738 Caocao's Bridges 图的割边
- HDU 4738 Caocao's Bridges(找割边)
- HDU 4738 Caocao's Bridges(求价值最小的桥)
- hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)
- HDU - 4738 Caocao's Bridges(桥)
- Hdu 4738 Caocao's Bridges (有重边无向图求桥)
- HDU 4738 Caocao's Bridges(重边无向图求桥)
- HDU 4738 Caocao's Bridges(tarjan算法)
- hdu 4738 Caocao's Bridges【求最小权值的桥】
- HDU 4738 Caocao's Bridges 桥+并查集
- HDU 4738 Caocao's Bridges(重边无向图求桥)
- hdu - 4738 Caocao's Bridges 割边
- hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)
- HDU-4738-Caocao's Bridges(割边)
- HDU 4738 — Caocao's Bridges 判桥
- hdu 4738 Caocao's Bridges (割边/桥)
- hdoj 4738 Caocao's Bridges 【无向图边-双联通 求所有桥中权值最小的】
- HDOJ 4738 - Caocao's Bridges 用tarjan找无向图的桥..注意trick