hdu4280(最大流)
传送门:Island Transport
题意:有N个岛屿 M条无向路 每个路有一最大允许的客流量,求从最西的那个岛屿最多能运用多少乘客到最东的那个岛屿。
分析:无向图正反都加弧,权值一样,这题点多,使用SAP优势大,点少时dinic好些。
dinic:8314ms
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <limits.h> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <stack> #include <vector> #include <set> #include <map> #define LL long long #define mod 100000000 #define inf 0x3f3f3f3f #define eps 1e-6 #define N 100010 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define PII pair<int,int> using namespace std; inline int read() { char ch=getchar(); int x=0,f=1; while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,vs,vt,tot; int pre[N],q[N],cur[N],h[N]; struct edge { int v,w,next; edge(){} edge(int v,int w,int next):v(v),w(w),next(next){} }e[N<<1]; void addedge(int u,int v,int w) { e[tot]=edge(v,w,pre[u]); pre[u]=tot++; e[tot]=edge(u,w,pre[v]); pre[v]=tot++; } void init() { memset(pre,-1,sizeof(pre)); tot=0; } /*******************dinic************************/ int bfs() { int head=0,tail=1; memset(h,-1,sizeof(h)); q[0]=vs;h[vs]=0; while(head!=tail) { int u=q[head++]; for(int i=pre[u];~i;i=e[i].next) { int v=e[i].v,w=e[i].w; if(w&&h[v]==-1) { h[v]=h[u]+1; q[tail++]=v; } } } return h[vt]!=-1; } int dfs(int u,int flow) { if(u==vt)return flow; int used=0; for(int i=cur[u];~i;i=e[i].next) { int v=e[i].v,w=e[i].w; if(h[v]==h[u]+1) { w=dfs(v,min(flow-used,w)); e[i].w-=w;e[i^1].w+=w; if(e[i].w)cur[u]=i; used+=w; if(used==flow)return flow; } } if(!used)h[u]=-1; return used; } int dinic() { int res=0; while(bfs()) { for(int i=1;i<=n;i++)cur[i]=pre[i]; res+=dfs(vs,inf); } return res; } /********************dinic***********************/ void build() { int u,v,w; int mx=-inf,mn=inf; n=read();m=read(); for(int i=1;i<=n;i++) { u=read();v=read(); if(u<mn)mn=u,vs=i; if(u>mx)mx=u,vt=i; } for(int i=1;i<=m;i++) { u=read();v=read();w=read(); addedge(u,v,w); } } int main() { int T; T=read(); while(T--) { init(); build(); printf("%d\n",dinic()); } }
SAP:2917ms
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#pragma comment(linker,"/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <limits.h> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <stack> #include <vector> #include <set> #include <map> #define LL long long #define mod 100000000 #define inf 0x3f3f3f3f #define eps 1e-6 #define N 100010 #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define PII pair<int,int> using namespace std; inline int read() { char ch=getchar(); int x=0,f=1; while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();} while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,m,vs,vt,tot,NV; int head[N],gap[N],level[N],q[N]; struct edge { int v,w,next; edge(){} edge(int v,int w,int next):v(v),w(w),next(next){} }e[N<<1]; void addedge(int u,int v,int w) { e[tot]=edge(v,w,head[u]); head[u]=tot++; e[tot]=edge(u,w,head[v]); head[v]=tot++; } void init() { memset(head,-1,sizeof(head)); tot=0; } /***************************SAP***********************/ void bfs(int vt) { memset(level,-1,sizeof(level)); memset(gap,0,sizeof(gap)); level[vt]=0; gap[level[vt]]++; queue<int>que; que.push(vt); while(!que.empty()) { int u=que.front(); que.pop(); for(int i=head[u]; i!=-1; i=e[i].next) { int v=e[i].v; if(level[v]!=-1)continue; level[v]=level[u]+1; gap[level[v]]++; que.push(v); } } } int pre[N]; int cur[N]; int SAP() { bfs(vt); memset(pre,-1,sizeof(pre)); memcpy(cur,head,sizeof(head)); int u=pre[vs]=vs,flow=0,aug=inf; gap[0]=NV; while(level[vs]<NV) { bool flag=false; for(int &i=cur[u]; i!=-1; i=e[i].next) { int v=e[i].v; if(e[i].w&&level[u]==level[v]+1) { flag=true; pre[v]=u; u=v; aug=min(aug,e[i].w); if(v==vt) { flow+=aug; for(u=pre[v]; v!=vs; v=u,u=pre[u]) { e[cur[u]].w-=aug; e[cur[u]^1].w+=aug; } aug=inf; } break; } } if(flag)continue; int minlevel=NV; for(int i=head[u]; i!=-1; i=e[i].next) { int v=e[i].v; if(e[i].w&&level[v]<minlevel) { minlevel=level[v]; cur[u]=i; } } if(--gap[level[u]]==0)break; level[u]=minlevel+1; gap[level[u]]++; u=pre[u]; } return flow; } /**************************SAP**********************/ void build() { int u,v,w; int mx=-inf,mn=inf; n=read();m=read(); NV=n+1; for(int i=1;i<=n;i++) { u=read();v=read(); if(u<mn)mn=u,vs=i; if(u>mx)mx=u,vt=i; } for(int i=1;i<=m;i++) { u=read();v=read();w=read(); addedge(u,v,w); } } int main() { int T; T=read(); while(T--) { init(); build(); printf("%d\n",SAP()); } }