UVA 12125 March of the Penguins topic: There are n on the grid ( n <= ) leaf blade with Ni-only penguin on the first piece of Lotus leaf ( 0<=ni<=ten )。 Due to limited capacity, the lotus leaf can only withstand mi ( 1<=mi<= The penguin jumps away from Mina. A penguin can jump D ( D<= ten 5 ) Unit distance. All penguins are asked to gather on the same piece of Lotus leaf. Ask which Lotus leaves can be a place for penguins to gather. Problem-solving: Why penguins don't swim ... Each leaf is a capacity, so each piece of lotus leaf to be split, split into two points, the capacity of the lotus leaf can withstand the maximum number of hops. Set a super source point, connect to all the lotus leaves, capacity for the number of initial penguins on the lotus leaf. Then enumerate each leaf as the starting point, see the last outflow of the maximum stream will be equal to the number of all penguins, will then record the current Lotus leaf (note: Lotus leaf number is starting from 0). The maximum flow per request remembers the flow of the initialized edge.
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace STD;Const intPO = the;Const intN = +;Const intM =20000;Const intINF =0x3f3f3f3f;typedef Long LongllintN, Rec[n], S, T, CNT, sum;DoubleLstructnode{intX, Y, A, B;} NODE[PO];structedge{intFrom, to, cap, flow; }; vector<Edge>Edges vector<int>G[M];voidInit () {sum =0; CNT =0; for(inti =0; i < M; i++) g[i].clear (); Edges.clear ();}voidAddedge (intFromintTo,intCAP) {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;}intDinic () {//dinic algorithm to find the maximum flow intAns =0; while(BFS ()) {memset(cur,0,sizeof(cur)); Ans + = DFS (s, INF); }returnAns;}DoubleGetdis (intXintY) {Doublepx = node[x].x, py = node[x].y;DoubleQX = node[y].x, qy = node[y].y;return POW(PX-QX,2) +POW(Py-qy,2);}voidInput () {s =0;scanf("%d%lf", &n, &l);DoubleA, B;intC, D; for(inti =1; I <= N; i++) {scanf("%lf%lf%d%d", &a, &b, &c, &d); sum + = C; Node[i] = (Node) {A, B, C, d}; Addedge (i, i + N, d); Addedge (S, I, c); } for(inti =1; I <= N; i++) { for(intj =1; J <= N; J + +) {if(i = = j)Continue;if(L * L-getdis (I, J) >1e-9) {Addedge (i + N, j, INF); } } }}voidClearf () { for(inti =0; I < edges.size (); i++) {Edges[i].flow =0; }}voidSolve () { for(inti =1; I <= N; i++) {clearf (); t = i;inttemp = Dinic ();if(temp = = SUM) {rec[cnt++] = i; } }}intMain () {intTscanf("%d", &t); while(t--) {init (); Input (); Solve ();if(!cnt)printf("-1");Else{printf("%d", rec[0] -1); for(inti =1; I < CNT; i++) {printf("%d", Rec[i]-1); } }puts(""); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint.
UVA 12125 March of the Penguins (Max Stream)