bzoj3396 [Usaco2009 Jan]Total flow 水流
2017-10-31 21:42
330 查看
题目
看一看题目描述,蜜汁熟悉,这,这不就是裸的最大流吗。
也好,马上noip了,复习一下。
看一看题目描述,蜜汁熟悉,这,这不就是裸的最大流吗。
也好,马上noip了,复习一下。
#include<bits/stdc++.h> #define N 100000 #define inf 10000000 using namespace std; int n,x,y,v; int first[N+5],nxt[N+5],to[N+5],fl[N+5],siz; int dis[N+5]; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; } inline int read() { int x=0,b=1; char c=nc(); for(;!(c<='9'&&c>='0');c=nc())if(c=='-')b=-1; for(;c<='9'&&c>='0';c=nc())x=x*10+c-'0'; return x*b; } inline char getc() { char c=nc(); for(;!((c<='z'&&c>='a')||(c<='Z'&&c>='A'));)c=nc(); return c; } inline bool BFS(int s,int t) { queue <int> Q; for(int i=0;i<=2000;i++)dis[i]=inf; dis[s]=0,Q.push(s); while(!Q.empty()) { int x=Q.front();Q.pop(); for(int i=first[x];i!=-1;i=nxt[i]) { int u=to[i]; if(fl[i]&&dis[x]+1<dis[u]) { dis[u]=dis[x]+1; if(u==t)return true; Q.push(u); } } } if(dis[t]==inf)return false;return true; } inline int DFS(int s,int f,int t) { if(s==t)return f; int rest=f; for(int i=first[s];i!=-1;i=nxt[i]) { int u=to[i]; if(rest&&fl[i]&&dis[u]==dis[s]+1) { int tmp=DFS(u,min(rest,fl[i]),t); if(tmp==0)dis[u]=0; fl[i]-=tmp; fl[i^1]+=tmp; rest-=tmp; } } return f-rest; } inline int Dinic(int s,int t) { int ans=0; while(BFS(s,t)) ans+=DFS(s,inf,t); return ans; } inline void add(int x,int y,int z) { nxt[siz]=first[x]; first[x]=siz; to[siz]=y; fl[siz]=z; siz++; } inline void insert(int x,int y,int z) { add(x,y,z),add(y,x,z); } int main() { freopen("in.txt","r",stdin); memset(first,-1,sizeof(first)); n=read(); for(int i=1;i<=n;i++) { x=getc(),y=getc(),v=read(); insert(x,y,v); } printf("%d",Dinic('A','Z')); return 0; }
相关文章推荐
- bzoj3396 [Usaco2009 Jan]Total flow 水流(最大流)
- bzoj 3396: [Usaco2009 Jan]Total flow 水流(最大流)
- 【BZOJ】3396: [Usaco2009 Jan]Total flow 水流 (最大流)
- 【网络流】【Dinic】【最大流】bzoj3396 [Usaco2009 Jan]Total flow 水流
- BZOJ3396: [Usaco2009 Jan]Total flow 水流
- 【BZOJ3396】【Usaco2009 Jan】Total flow 水流
- bzoj 3396: [Usaco2009 Jan]Total flow 水流 网络流
- [BZOJ3396][Usaco2009 Jan]Total flow 水流(最大流)
- bzoj 3396: [Usaco2009 Jan]Total flow 水流【最大流】
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- bzoj3396 [Usaco2009 Jan]Total flow 水流
- bzoj 3396 [Usaco2009 Jan]Total flow 水流
- 3396: [Usaco2009 Jan]Total flow 水流
- 3396: [Usaco2009 Jan]Total flow 水流 (最大流)
- BZOJ3396 [Usaco2009 Jan]Total flow 水流
- BZOJ3396: [Usaco2009 Jan]Total flow 水流 网络流
- [bzoj1574][Usaco2009 Jan]地震损坏Damage_dfs
- 【bzoj1576】[Usaco2009 Jan]安全路经Travel
- bzoj1575 [Usaco2009 Jan]气象牛Baric