2015 Multi-school joint training first field tricks Device (hdu5294)

Source: Internet
Author: User

Test instructions: Give a graph, give the starting point S, the end point T, the minimum to remove a few edges so that s can not t, up to a few edges so that s to T
Ideas:
First, run one side shortest way, record shortest number of sides in short circuit, total number of sides-the shortest number of sides is the second answer
The first answer is to ask for the smallest cut in the shortest way, that is, to find the maximum flow, and then build a new map based on the short circuit, right 1, run one side of the network stream

Template problem, the template is used in the future

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace STD;Const intINF =0x7f7f7f;Const intMAXM =12e4+5;Const intMAXN =2e3+5;intN, M;structEdge {intTo, W, next;} EDGE[MAXM];intTot, HEAD[MAXN];voidAddedge (intUintVintW) {edge[tot].to = v;    EDGE[TOT].W = W;    Edge[tot].next = Head[u];    Head[u] = tot++;    edge[tot].to = u;    EDGE[TOT].W = W;    Edge[tot].next = Head[v]; HEAD[V] = tot++;}intDIS[MAXN], VIS[MAXN];intMINB[MAXN];intSPFA (ints) {memset(Dis,0x3f,sizeof(dis));memset(Vis,0,sizeof(VIS));memset(MINB,0x3f,sizeof(MINB)); Queue<int>Q Dis[s] =0; Minb[s] =0; Vis[s] =1; Q.push (s); while(!q.empty ()) {intU = Q.front ();        Q.pop (); Vis[u] =0; for(inti = Head[u]; ~i; i = edge[i].next) {intv = edge[i].to, w = EDGE[I].W;if(Dis[v] = = Dis[u] + W) {Minb[v] = min (Minb[v], Minb[u] +1);if(!vis[v]) {Vis[v] =1;                Q.push (v); }            }if(Dis[v] > Dis[u] + W)                {Dis[v] = Dis[u] + W; MINB[V] = Minb[u] +1;if(!vis[v]) {Vis[v] =1;                Q.push (v); }            }        }    }}structEg {intU, cap, rev; Eg (intUuintCcintRR) {u = uu; cap = cc; rev = RR; }}; vector<Eg>G[MAXN];BOOLUSED[MAXN];voidAddintUintVintCAP) {G[u].push_back (Eg (V, Cap, g[v].size ())); G[v].push_back (Eg (U,0, G[u].size ()-1));}voidBuild () { for(inti =1; I <= N; ++i) { for(intj = Head[i]; ~j; j = edge[j].next) {intv = edge[j].to, w = EDGE[J].W;if(Dis[v]-dis[i] = = W) {Add (I, V,1); }        }    }}intDfsintVintTintf) {if(v = = t)returnF USED[V] =true; for(inti =0; I < g[v].size (); ++i) {Eg &e = g[v][i];if(!USED[E.U] && e.cap >0) {intD = DFS (E.U, T, Min (f, e.cap));if(D >0) {e.cap-= D; G[e.u][e.rev].cap + = D;returnD }        }    }return 0;}intMax_flow (intSintT) {intFlow =0; while(1) {memset(Used,0,sizeof(used));intF = DFS (S, T, INF);if(f = =0)returnFlow    Flow + + F; }}voidInit () {tot =0;memset(Head,-1,sizeof(head)); for(inti =0; I <= N;    ++i) {g[i].clear (); }}intMain () {//freopen ("in", "R", stdin);     while(~scanf("%d%d", &n, &m)) {init (); for(inti =0; I < m; ++i) {intU, V, W;scanf(" %d%d%d", &u, &v, &w);        Addedge (U, V, W); } SPFA (1); Build ();intAns = max_flow (1, n);printf("%d%d\n", ans, m-minb[n]); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

2015 Multi-school joint training first field tricks Device (hdu5294)

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.