Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 64640 | Accepted: 24923 |
Description
Input
Output
Sample Input
5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
Source
Dinic模板
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<queue> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=k;i>=n;i--) #define LL long long #define pa pair<int,int> #define MAXN 205 #define INF 1000000000 using namespace std; int n,m,s,t,x,y,c,cnt=0,ans,dis[MAXN],cur[MAXN],head[MAXN]; struct edge_type { int next,to,v; }e[MAXN*2]; inline int read() { int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void add_edge(int x,int y,int v) { e[++cnt]=(edge_type){head[x],y,v};head[x]=cnt; e[++cnt]=(edge_type){head[y],x,0};head[y]=cnt; } inline bool bfs() { queue<int> q; memset(dis,-1,sizeof(dis)); dis[s]=0;q.push(s); while (!q.empty()) { int tmp=q.front();q.pop(); if (tmp==t) return true; for(int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1) { dis[e[i].to]=dis[tmp]+1; q.push(e[i].to); } } return false; } inline int dfs(int x,int f) { int tmp,sum=0; if (x==t) return f; for(int i=cur[x];i;i=e[i].next) { int y=e[i].to; if (e[i].v&&dis[y]==dis[x]+1) { tmp=dfs(y,min(f-sum,e[i].v)); e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp; if (e[i].v) cur[x]=i; if (sum==f) return sum; } } if (!sum) dis[x]=-1; return sum; } inline void dinic() { ans=0; while (bfs()) { F(i,s,t) cur[i]=head[i]; ans+=dfs(s,1<<30); } return; } int main() { while (~scanf("%d",&m)) { memset(head,0,sizeof(head)); n=read(); cnt=1;s=1;t=n; F(i,1,m) { x=read();y=read();c=read(); add_edge(x,y,c); } dinic(); printf("%d\n",ans); } }