1. 程式人生 > >HDU 4280 Island Transport(無向圖最大流)

HDU 4280 Island Transport(無向圖最大流)

clear ofa pri img size http algorithm caller end

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

題意:

  比較裸的最大流題目,就是這是個無向圖,並且比較卡時間。

思路:

  是這樣的,由於是無向圖,所以addedge 的反邊容量直接設為原始流量。然後還可以優化搜索的方向,bfs可以從t到s跑,dfs可以從s到t跑,這樣快。

技術分享圖片
//#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 namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//這是一個大根堆q //priority_queue<int,vector<int>,greater<int> >q;//這是一個小根堆q #define fi first #define se second //#define endl ‘\n‘ #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用來壓行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黃金分割點 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<0||ch>9) f|=(ch==-),ch=getchar(); while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ struct edge{ int u,v,cap; edge(){} edge(int u,int v,int cap): u(u),v(v),cap(cap) {} }es[200009]; int tot,s,t; vector<int>tab[100009]; int dis[100009],cur[100009]; void addedge(int u,int v,int cap){ tab[u].pb(tot); es[tot++] = edge(u,v,cap); tab[v].pb(tot); es[tot++] = edge(v,u,cap); } bool bfs(){ queue<int>q;q.push(t); memset(dis,inf,sizeof(dis)); dis[t] = 1; while(!q.empty()){ int h = q.front();q.pop(); for(int i=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); } } } return dis[s] < inf; } int dfs(int x,int maxflow){ if(x == t || maxflow == 0) return maxflow; for(int i=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){ int flow = dfs(e.v, min(maxflow, e.cap)); if(flow){ e.cap -= flow; es[tab[x][i] ^ 1].cap += flow; return flow; } } } return 0; } int dinic(){ int ans = 0; while(bfs()){ int flow; memset(cur,0,sizeof(cur)); do{ flow = dfs(s,inf); if(flow) ans += flow; }while(flow); } return ans; } int main(){ int T;scanf("%d", &T); while(T--){ int n,m,x,y,nx = inf,ny = -inf; // scanf("%d%d",&n, &m); read(n),read(m); for(int i=1; i<=n; i++)tab[i].clear(); tot = 0; for(int i=1; i<=n; i++){ int x,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(int i=1; i<=m; i++){ int u,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(無向圖最大流)