HDU 5294 Tricks device title: Wu Evil in the tomb Chase Zhang Ling, there are many roads in the tomb, walk each road need a time, Wu evil only in the shortest time from the entrance of the tomb to reach the exit to catch up with a spirit. But Zhang Ling is very powerful, he can use the odd gate dun armor to make a road impassable. Now, asked, Zhang Ling at least a few ways to make Wu evil can not catch up with him, and in the case of Wu evil can catch up with a spirit, Zhang Ling can block the maximum number of roads. Problem-solving ideas: The first to find the most short-circuit in the tomb (the least time-consuming road), and then find the shortest path at the same time, to record the minimum road to pass by which several roads and to walk the least through the way. The most blocked road is the total number of ways minus the shortest way to go through the least. Then according to the shortest path through the road, to build a graph of network flow, to find the maximum flow, is the minimum need to block the road.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;typedef Long LongllConst intINF =0x3f3f3f3f;Const intN =20005;intN, M, S, t;structEdgeS {intFrom, to, Dist; }; vector<EdgeS>edgess; vector<int>Gs[n];intViss[n], Ds[n], rec[n];voidInits () {memset(Viss,0,sizeof(Viss)); for(inti =0; i < N; i++) gs[i].clear (); Edgess.clear (); }voidAddedges (intFromintTo,intDist) {Edgess.push_back ((EdgeS) {from, To, Dist}); Edgess.push_back ((EdgeS) {To, from, Dist});intpos = Edgess.size (); Gs[from].push_back (POS-2); Gs[to].push_back (POS-1);}voidSPFA () {memset(Viss,0,sizeof(Viss)); for(inti =0; i < N; i++) ds[i] = INF; for(inti =0; i < N; i++) Rec[i] = INF; Ds[s] =0; Rec[s] =0; Viss[s] =1; Queue<int>Q; Q.push (s); while(! Q.empty ()) {intU = Q.front (); Q.pop (); Viss[u] =0; for(inti =0; I < gs[u].size (); i++) {intv = edgess[gs[u][i]].to;if(Ds[v] > Ds[u] + edgess[gs[u][i]].dist) {Ds[v] = Ds[u] + edgess[gs[u][i]].dist; REC[V] = Rec[u] +1;if(!viss[v]) {Viss[v] =1; Q.push (v); } }Else if(Ds[v] = = Ds[u] + edgess[gs[u][i]].dist) {if(Rec[v] > Rec[u] +1) {Rec[v] = Rec[u] +1;if(!viss[v]) {Viss[v] =1; Q.push (v); } } } } } }structedge{intFrom, to, cap, flow; }; vector<Edge>Edges vector<int>G[n];voidInit () { for(inti =0; i < N; i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo,intCapintFlow) {Edges.push_back (Edge) {from, to, Cap,0}); Edges.push_back (Edge) {To, from,0,0});intm = Edges.size (); G[from].push_back (M-2); G[to].push_back (M-1);}intVis[n], d[n];intBFS () {memset(Vis,0,sizeof(VIS)); Queue<int>Q; Q.push (s); D[s] =0; Vis[s] =1; while(! Q.empty ()) {intU = Q.front (); Q.pop (); for(inti =0; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(!vis[e.to] && e.cap > E.flow) {Vis[e.to] =1; D[e.to] = D[u] +1; Q.push (e.to); } } }returnVIS[T];}intCur[n];intDFS (intUintA) {if(U = = T | | a = =0)returnAintFlow =0, F; for(int&i = Cur[u]; I < g[u].size (); i++) {Edge &e = edges[g[u][i]];if(D[u] +1= = D[e.to] && (f = DFS (e.to, Min (A, e.cap-e.flow))) >0) {E.flow + = f; edges[g[u][i]^1].flow-= f; Flow + + F; A-= f;if(A = =0) Break; } }returnFlow;}intMF () {//dinic algorithm to find the maximum flow intAns =0; while(BFS ()) {memset(cur,0,sizeof(cur)); Ans + = DFS (s, INF); }returnAns;}intMain () { while(scanf("%d%d", &n, &m) = =2) {intU, V, D; s =0, t = n-1; Init (); Inits (); for(inti =0; I < m; i++) {scanf(" %d%d%d", &u, &v, &d);if(U = = v)Continue; Addedges (U-1, V-1, d); } SPFA (); for(inti =0; I < edgess.size (); i++) {if(ds[edgess[i].to] = = Ds[edgess[i].from] + edgess[i].dist) {Addedge (Edgess[i].from, Edgess[i].to,1,0); } }printf("%d%d\n", MF (), M-rec[n-1]); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5294 Tricks Device (shortest circuit + maximum flow)