HDU 4280 Island Transport (maximum flow without graph)

Source: Internet
Author: User

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

Test instructions

Compare bare maximum flow problem, that is, this is an no-map, and compare card time.

Ideas:

So, because it is an addedge graph, the inverse edge capacity is set directly to the original flow. Then you can also optimize the direction of the search, BFS can run from T to S, DFS can run from S to T, so fast.

//#pragma GCC optimize (3)//#pragma COMMENT (linker, "/stack:102400000,102400000")//C + +//#pragma GCC diagnostic error "-std=c++11"//#pragma COMMENT (linker, "/stack:200000000")//#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")//#pragma GCC optimize ("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations, -fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions, Inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions, no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,- falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,- fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,- fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,ofast,inline,- Fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2 ", 3)#include<algorithm>#include<iterator>#include<iostream>#include<cstring>#include<cstdlib>#include<iomanip>#include<bitset>#include<cctype>#include<cstdio>#include<string>#include<vector>#include<stack>#include<cmath>#include<queue>#include<list>#include<map>#include<Set>#include<cassert>using namespacestd;#defineLson (L, Mid, RT << 1)#defineRson (mid + 1, R, RT << 1 | 1)#defineDebug (x) cerr << #x << "=" << x << "\ n";#definePB Push_back#definePQ Priority_queuetypedefLong Longll;typedef unsignedLong Longull;//typedef __int128 BLL;typedef PAIR&LT;LL, LL >Pll;typedef pair<int,int>Pii;typedef pair<int,pii>P3;//priority_queue<int> Q;//This is a big root heap Q//priority_queue<int,vector<int>,greater<int> >q;//This is a little Gan q#defineFi first#defineSe Second//#define Endl ' \ n '#defineOKC Ios::sync_with_stdio (false); Cin.tie (0)#defineFT (A,B,C) for (int a=b; A <= C;++a)//used to press the line#defineREP (I, J, K) for (int i = j; i < K; ++i)#defineMax3 (A,B,C) max (max (b), c);//Priority_queue<int, Vector<int>, greater<int> >que;Constll mos =0x7FFFFFFF;//2147483647Constll nMOS =0x80000000;//-2147483648Const intINF =0x3f3f3f3f;Constll INFF =0x3f3f3f3f3f3f3f3f;// -Const intMoD = 1e9+7;Const DoubleESP = 1e-8;Const DoublePi=acos (-1.0);Const DoublePhi=0.61803399;//Golden Split PointConst DoubleTphi=0.38196601; template<typename t>inline T read (t&x) {x=0;intf=0;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') f|= (ch=='-'), ch=GetChar ();  while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); returnx=f?-x:x;}/*-----------------------Showtime----------------------*/            structedge{intU,v,cap; Edge () {} Edge (intUintVintcap): U (U), V (v), Cap (CAP) {}}es[200009]; inttot,s,t; Vector<int>tab[100009]; intdis[100009],cur[100009]; voidAddedge (intUintVintcap)                {TAB[U].PB (TOT); Es[tot++] =Edge (U,V,CAP);                TAB[V].PB (TOT); Es[tot++] =Edge (V,U,CAP); }            BOOLBFs () {Queue<int>Q;q.push (t); memset (Dis,inf,sizeof(DIS)); Dis[t]=1;  while(!Q.empty ()) {                    inth =Q.front (); Q.pop ();  for(intI=0; I<tab[h].size (); i++) {Edge& e =Es[tab[h][i]]; if(es[tab[h][i]^1].cap>0&& DIS[E.V] >=inf) {DIS[E.V]= Dis[h] +1;                        Q.push (E.V); }                    }                }                returnDis[s] <inf; }            intDfsintXintMaxflow) {                if(x = = T | | maxflow = =0)returnMaxflow;  for(intI=CUR[X]; I<tab[x].size (); i++) {Cur[x]=i; Edge& e =Es[tab[x][i]]; if(DIS[E.V] = = Dis[x]-1&& E.cap >0){                        intFlow =dfs (e.v, min (Maxflow, e.cap)); if(flow) {E.cap-=flow; Es[tab[x][i]^1].cap + =flow; returnflow; }                    }                }                return 0; }            intDinic () {intAns =0;  while(BFS ()) {intflow; memset (cur,0,sizeof(cur));  Do{Flow=DFS (S,inf); if(flow) ans + =flow; } while(flow); }                returnans; }intMain () {intT;SCANF ("%d", &T);  while(t--){                intN,m,x,y,nx = Inf,ny =-inf; //scanf ("%d%d", &n, &m);read (n), read (m);  for(intI=1; i<=n; i++) tab[i].clear (); Tot=0;  for(intI=1; i<=n; i++){                    intx, y; //scanf ("%d%d", &x, &y);read (x), read (y); if(x < NX) s = i, NX =x; if(x > NY) t = i, NY =x; }                //debug (s); //Debug (t);                 for(intI=1; i<=m; i++){                    intu,v,w; //scanf ("%d%d%d", &u, &v, &w);Read ( u), read (v), read (w);                   Addedge (U, V, W); //Addedge (V, U, W);} printf ("%d\n", Dinic ()); }            return 0;}
HDU 4280

HDU 4280 Island Transport (maximum flow without graph)

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.