HDU 4738 Caocao ' s Bridges (bridge min weight + de-weight)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=4738

The main idea of the main topic: there are some islands are connected by the bridge, each seat is guarded by soldiers, Zhou Yu wanted to divide the islands into two parts, but he can only blow up a bridge, asked to send a few soldiers to go; If you cannot complete output-1

1: If these islands are not connected, you do not need to send people to

2: If the bridge's guard is 0, you'll have to send a man to blow it up.

3: If output cannot be completed-1

4: Output minimum number of people to be sent

#include <stdio.h>#include<string.h>#include<math.h>#include<queue>#include<algorithm>using namespacestd;#defineN 1100#defineINF 0XFFFFFFFintDfn[n], low[n], head[n], f[n];intTime , M, N, Min, CNT;structedge{intNext, U, V, c;} Edge[n*N];voidInit () {memset (head,-1,sizeof(head)); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); Memset (F,0,sizeof(f)); time= CNT =0;}voidAddedge (intUintVintc) {edge[cnt].u=u; EDGE[CNT].V=v; EDGE[CNT].C=C; Edge[cnt].next=Head[u]; Head[u]= cnt++;}voidTarjan (intUintFA) {    intI, V, flag =0; Low[u]= Dfn[u] = + +Time ; F[u]=FA;  for(i = head[u]; I! =-1; i =Edge[i].next) {v=edge[i].v; if(v = = FA &&!)flag) {Flag=1; Continue; }//go to the heavy side and skip if there are heavy edges        if(!Dfn[v])            {Tarjan (V, u); Low[u]=min (Low[u], low[v]); if(Dfn[u] <Low[v]) Min=min (min, edge[i].c); }        ElseLow[u]=min (Low[u], dfn[v]); }}intMain () {inti, U, V, c, K;  while(SCANF ("%d%d", &m, &n), M +N) {Init (); K=0;  while(n--) {scanf ("%d%d%d", &u, &v, &c);            Addedge (U, V, c);        Addedge (V, U, c); } Min=INF;  for(i =1; I <= m; i++)        {            if(!Dfn[i]) {Tarjan (i,-1); K++; }        }        if(k >1) {printf ("0\n"); Continue; }//1        if(Min = =0) printf ("1\n");//2        Else if(Min = =INF) printf ("-1\n");//3        Elseprintf ("%d\n", Min);//4    }    return 0;}
View Code

HDU 4738 Caocao ' s Bridges (bridge min weight + de-weight)

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.