SGU 176Flow construction 有源汇上下界网络流 最小流
来源:互联网 发布:网络美工是做什么的 编辑:程序博客网 时间:2024/05/01 14:44
SGU 176 Flow construction 有源汇上下界网络流 最小流
题意:有n个节点m根水管的网络,每根水管有个水流的限制,问最少要多少水流能满足整个网络。
最小流
代码:
//author: CHC//First Edit Time: 2015-09-10 10:45#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <set>#include <vector>#include <map>#include <queue>#include <set>#include <algorithm>#include <limits>using namespace std;typedef long long LL;const int MAXN=110;const int MAXM=1e+5;const int INF = numeric_limits<int>::max();const LL LL_INF= numeric_limits<LL>::max();struct Edge { int to,ci,next; Edge(){} Edge(int _to,int _ci,int _next):to(_to),ci(_ci),next(_next){}}e[MAXM];int head[MAXN],tot;void init(){ memset(head,-1,sizeof(head)); tot=0;}void AddEdge(int u,int v,int ci0,int ci1=0){ e[tot]=Edge(v,ci0,head[u]); head[u]=tot++; e[tot]=Edge(u,ci1,head[v]); head[v]=tot++;}int dis[MAXN],sta[MAXN],top,cur[MAXN];bool bfs(int st,int et){ memset(dis,0,sizeof(dis)); dis[st]=1; queue <int> q;q.push(st); while(!q.empty()){ int now=q.front();q.pop(); for(int i=head[now];~i;i=e[i].next) if(e[i].ci&&!dis[e[i].to]){ dis[e[i].to]=dis[now]+1; q.push(e[i].to); } } return dis[et];}LL Dinic(int st,int et){ LL ans=0; while(bfs(st,et)){ top=0; memcpy(cur,head,sizeof(cur)); int u=st,i; while(1){ if(u==et){ int pos,minn=INF; for(i=0;i<top;i++){ if(minn>e[sta[i]].ci){ minn=e[sta[i]].ci; pos=i; } } for(i=0;i<top;i++){ e[sta[i]].ci-=minn; e[sta[i]^1].ci+=minn; } top=pos; u=e[sta[top]^1].to; ans+=minn; } for(i=cur[u];~i;cur[u]=i=e[i].next) if(e[i].ci&&dis[u]+1==dis[e[i].to])break; if(cur[u]!=-1){ sta[top++]=cur[u]; u=e[cur[u]].to; } else { if(top==0)break; dis[u]=0; u=e[sta[--top]^1].to; } } } return ans;}int du[MAXN],n,m,num[MAXM],base[MAXM];int main(){ while(~scanf("%d%d",&n,&m)){ memset(du,0,sizeof(du)); init(); for(int i=0,x,y,ci,v;i<m;i++){ scanf("%d%d%d%d",&x,&y,&ci,&v); num[i]=tot; if(!v){ AddEdge(x,y,ci); base[i]=0; } else { AddEdge(x,y,0); du[x]-=ci;du[y]+=ci; base[i]=ci; } } int sst=0,eet=n+1; LL ctot=0; for(int i=1;i<=n;i++) if(du[i]>0)AddEdge(sst,i,du[i]),ctot+=du[i]; else AddEdge(i,eet,-du[i]); LL ans=Dinic(sst,eet); AddEdge(n,1,INF); //ans+=Dinic(sst,eet); LL t=Dinic(sst,eet); if(ans+t!=ctot)puts("Impossible"); else { //ans=Dinic(1,n); //printf("%d\n",e[tot-1].ci); printf("%I64d\n",t); for(int i=0;i<m-1;i++){ printf("%d ",base[i]+e[num[i]^1].ci); } printf("%d\n",base[m-1]+e[num[m-1]^1].ci); } } return 0;}
0 0
- SGU 176Flow construction 有源汇上下界网络流 最小流
- sgu 176 Flow construction--有源汇 有上下界 最小流
- SGU 176 Flow construction 有源汇 有上下界的最小流
- sgu 176 Flow construction(有源汇上下界最小流)
- SGU - 176 Flow construction(上下界有源有汇最小流)
- SGU 176 Flow construction(有源有汇上下界最小流)
- sgu 176 Flow construction(有源汇有上下界最小流)
- SGU 176 Flow construction 有源汇有上下界的最小流
- sgu176. Flow construction 有源汇上下界最小流
- 【sgu176】Flow construction 有源汇上下界最小流
- sgu 176 Flow construction 上下界最小流
- SGU 176 Flow construction-上下界网络流
- SGU 176 Flow construction 网络流 有上下界限制最小流
- [上下界最小流] SGU 176. Flow construction
- 【有源汇有上下界最小流】SGU-176 Flow constructions
- ★ SGU 176 有源汇上下界最小流
- SGU 176 有源汇上下界最小流
- SGU 176 有源汇上下界的最小流
- dispatchTouchEvent小分析
- iGrimaceV8 V8在线威锋源apt.so/qwkjv8手机直接下载安装教程图:
- mysql的中varchar
- webserver日志中\xCF\xE3\xB8\xDB\xCA\xAE\xB4\xF3\xCE\xC4\xBB\xAF\xB7\xFB\xBA\xC5这种形式原因及处理
- 将jpeg图片显示在framebuffer上
- SGU 176Flow construction 有源汇上下界网络流 最小流
- Maven多工程依赖在Eclipse中发布到Server被依赖工程没有生成jar的问题
- Unity——MVC(很不错的解释应用)
- Jquery中替换节点的方法replaceWith()和replaceAll()
- FRAMEBUFFER 显示原理及RGB显示
- 衡阳哪个微信公众号最好?
- JavaScript基础回顾
- 机器学习实战2:k近邻算法KNN(python)
- 【NOIP2013-D1T3】货车运输