POJ1273
来源:互联网 发布:fab法则 美工 编辑:程序博客网 时间:2024/06/15 23:48
Problem : Drainage Ditches
Descripition : 有一天下雨了,然后从1到N排水,有M条通道,每条通道有一个最大排水量,现在问你最多能排多少水。
Solution : 裸最大网络流。
Code(C++) :
#include <stdio.h>#include <string.h>#include <iostream>#include <string>#include <queue>#define MIN(a,b) ((a)>(b)? (b):(a))using namespace std;const int SIZE=400+50;const int INF=0x3f3f3f3f;struct Node{ int u,v; int cap; Node(){} Node(int U,int V,int Cap): u(U),v(V),cap(Cap){}};int src,des;vector<int> G[SIZE];Node e[SIZE*SIZE/2];int d[SIZE];int cur[SIZE];int n;int top;int M,N;void addedge(int u,int v,int cap){ G[u].push_back(top); e[top++]=Node(u,v,cap); G[v].push_back(top); e[top++]=Node(v,u,0);}void build(){ int i,j; for(i=0;i<SIZE;i++) G[i].clear(); top=0; n=N; src=0; des=N+1; int x,y,c; for(i=1;i<=M;i++){ scanf("%d%d%d",&x,&y,&c); addedge(x,y,c); } addedge(src,1,INF); addedge(n,des,INF);}bool bfs(int src,int des){ memset(d,-1,sizeof(d)); queue<int> que; que.push(src); d[src]=0; while(!que.empty()){ int now=que.front(); que.pop(); for(int i=0;i<G[now].size();i++){ Node &tmp=e[G[now].at(i)]; if(tmp.cap>0&&d[tmp.v]==-1) d[tmp.v]=d[now]+1, que.push(tmp.v); } } return d[des]>=0;}int dfs(int t,int sum,int des){ if(t==des||!sum) return sum; int flow=0,f; for(int &i=cur[t];i<G[t].size();i++){ Node &tmp=e[G[t].at(i)]; if(d[tmp.v]==d[t]+1&&(f=dfs(tmp.v,MIN(sum,tmp.cap),des))>0){ tmp.cap-=f; e[G[t].at(i)^1].cap+=f; sum-=f; flow+=f; if(!sum) break; } } return flow;}int DINIC(int src,int des){ int sum=0; while(bfs(src,des)){ memset(cur,0,sizeof(cur)); sum+=dfs(src,INF,des); } return sum;}void work(){ build(); printf("%d\n",DINIC(src,des));}int main(){ //freopen("in.data","r",stdin); while(~scanf("%d%d",&M,&N)) work(); return 0;}
0 0
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- poj1273
- POJ1273
- poj1273
- POJ1273
- poj1273 EK
- POJ1273 Drainage Ditches
- POJ1273(最大流)
- poj1273 Drainage Ditches
- webstorm调试node(express 框架)
- day08Python中的类成员
- mongoDB查询
- redis如何后台启动
- 剩余类和完全剩余系(简单理解)
- POJ1273
- 使用邻接表存图并遍历
- Android源码编译--版本查询
- LeetCode OJ-121. Best Time to Buy and Sell Stock(股票问题)
- Quartz-作业调度框架
- 172.leetcode Factorial Trailing Zeroes(easy)[阶乘 数学]
- JavaWeb学习总结——Web应用中使用JavaMail发送邮件
- 为什么CSS选择器是从右往左解析
- 安装完 MySQL 后必须调整的 10 项配置