标签:lin ace cst view namespace name ext head inf
★★☆ 输入文件:maxflowd.in
输出文件:maxflowd.out
简单对比
时间限制:1 s 内存限制:128 MB
#include <cstdio> #include <queue> #define inf 0x7fffffff #define N 105 using namespace std; bool vis[N]; int n,s,t,fa[N],dis[N],flow[N],cnt=1,head[N]; struct Edge { int next,to,flow,cost; Edge (int next=0,int to=0,int flow=0,int cost=0) : next(next),to(to),flow(flow),cost(cost) {} }edge[N*N]; inline void ins(int u,int v,int w,int l) { edge[++cnt]=Edge(head[u],v,w,l); head[u]=cnt; } bool spfa(int s,int t) { for(int i=s;i<=t;++i) vis[i]=0,flow[i]=inf,dis[i]=inf; dis[s]=0; fa[s]=0; queue<int>q; q.push(s); for(int now;!q.empty();) { now=q.front(); q.pop(); vis[now]=0; for(int i=head[now];i;i=edge[i].next) { int v=edge[i].to; if(dis[v]>dis[now]+edge[i].cost&&edge[i].flow) { dis[v]=dis[now]+edge[i].cost; flow[v]=min(flow[now],edge[i].flow); fa[v]=i; if(!vis[v]) { q.push(v); vis[v]=1; } } } } return dis[t]!=inf; } int dinic(int s,int t) { int ans=0; for(;spfa(s,t);) { int x=flow[t]; for(int i=t;i!=s&&i;i=edge[fa[i]^1].to) { edge[fa[i]].flow-=x; edge[fa[i]^1].flow+=x; } ans+=dis[t]*x; } return ans; } int main() { freopen("maxflowd.in","r",stdin);freopen("maxflowd.out","w",stdout); scanf("%d%d%d",&n,&s,&t); for(int i=1;i<=n;++i) for(int a,b,j=1;j<=n;++j) { scanf("%d%d",&a,&b); if(a&&b) { ins(i,j,a,b); ins(j,i,0,-b); } } printf("%d\n",dinic(s,t)); return 0; }
标签:lin ace cst view namespace name ext head inf
原文地址:http://www.cnblogs.com/ruojisun/p/7436145.html