Click to open link
Test instructions: Give an undirected graph and the weight of each edge, now destroy a road so that the diagram is no longer connected, can only destroy a road, ask the minimum cost, spend the weight on the edge, note that the figure may be disconnected at first, output 1; the least expensive side is 0 output 1, nothing else.
Idea: To find a bridge template problem, in finding a bridge to update the cost of the smallest, on the other may have a heavy side, simple ~ ~ ~
#include <vector> #include <stdio.h> #include <string.h> #include <stdlib.h> #include < Iostream> #include <algorithm>using namespace std;typedef long long ll;const int Inf=0x3f3f3f3f;const int maxn= 1010;struct edge{int to,cost; Edge (int a,int b) {to=a;cost=b;}}; Vector<edge>g[maxn];int l[maxn],e[maxn],stack1[maxn],vis[maxn];int V[maxn][maxn],cnt,vv[maxn];int N,m,k,kk, max1,top;void dfs (int x,int fa) {vis[x]=1; L[x]=k; e[x]=k++; for (unsigned int i=0;i<g[x].size (); i++) {Edge e=g[x][i]; if (!vis[e.to]) {DFS (e.to,x); L[x]=min (l[x],l[e.to]); if (l[e.to]>e[x]&&v[x][e.to]==1) {max1=min (max1,e.cost); kk++; }}else if (E.TO!=FA) l[x]=min (l[x],e[e.to]); }}int Tarjan () {K=0;kk=0;dfs (); return KK;} void dfs1 (int x) {vv[x]=1;cnt++; for (unsigned int i=0;i<g[x].size (); i++) {Edge e=g[x][i]; if (!vv[e.to]) DFS1 (e.to);}}int Main () {int a,b,c; while (scanf ("%d%d", &n,&m)!=-1) {if (n==0&&m==0) break; for (int i=0;i<maxn;i++) {g[i].clear (); vis[i]=0; e[i]=0;vv[i]=0; } memset (V,0,sizeof (v)); for (int i=0;i<m;i++) {scanf ("%d%d%d", &a,&b,&c); G[a].push_back (Edge (b,c)); G[b].push_back (Edge (a,c)); v[a][b]++;v[b][a]++; } Cnt=0;max1=inf; DFS1 (1); if (cnt!=n) {printf ("0\n"); Continue } int Ans=tarjan (); if (ans==0) printf (" -1\n"); else if (max1==0) printf ("1\n"); else printf ("%d\n", max1); } return 0;}
HDU 4738 Dual Unicom components