HDU 4738 Dual Unicom components

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.