Drainage Ditches

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 76000   Accepted: 29530

Description


Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch.
Farmer John knows not only how many gallons of water each ditch can
transport per minute but also the exact layout of the ditches, which
feed out of the pond and into each other and stream in a potentially
complex network.

Given all this information, determine the maximum rate at which
water can be transported out of the pond and into the stream. For any
given ditch, water flows in only one direction, but there might be a way
that water can flow in a circle.

Input


The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output


For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input


5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output


50

Source



/**
题目:poj1273 Drainage Ditches
链接:http://poj.org/problem?id=1273
题意:裸的最大流
思路:裸的最大流 */
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;
struct edge{
int to, cap, rev;
};
vector<edge> G[N];
bool used[N];
void add_edge(int from,int to,int cap)
{
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,,G[from].size()-}); }
int dfs(int v,int t,int f)
{
if(v==t) return f;
used[v] = true;
for(int i = ; i < G[v].size(); i++){
edge&e = G[v][i];
if(!used[e.to]&&e.cap>){
int d = dfs(e.to,t,min(f,e.cap));
if(d>){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
}
LL max_flow(int s,int t)
{
LL flow = ;
for(;;){
memset(used, , sizeof used);
int f = dfs(s,t,INF);
if(f==) return flow;
flow+=f;
}
}
int main()
{
int n , m;
while(scanf("%d%d",&m,&n)==)
{
int u, v, cap;
for(int i = ; i <= n; i++) G[i].clear(); for(int i = ; i < m; i++){
scanf("%d%d%d",&u,&v,&cap);
add_edge(u,v,cap);
}
printf("%lld\n",max_flow(,n));
}
return ;
}
/**
题目:poj1273 Drainage Ditches
链接:http://poj.org/problem?id=1273
题意:
思路:Dinic算法解最大流 */
#include<iostream>
#include<cstring>
#include<vector>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;
struct Edge{
int from, to, cap, flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct Dinic{
int n, m, s, t;
vector<Edge> edges;
vector<int> G[N];
bool vis[N];
int d[N];
int cur[N]; void init(int n)
{
this->n = n;
for(int i = ; i <= n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS(){
memset(vis, , sizeof vis);
queue<int> Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++){
Edge &e = edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to] = ;
d[e.to] = d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a){
if(x==t||a==) return a;
int flow = , f;
for(int &i = cur[x]; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>){
e.flow += f;
edges[G[x][i]^].flow -= f;
flow += f;
a -= f;
if(a==) break;
}
}
return flow;
} int Maxflow(int s,int t){
this->s = s, this->t = t;
int flow = ;
while(BFS()){
memset(cur, , sizeof cur);
flow += DFS(s,INF);
}
return flow;
}
};
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)==){
int from, to, cap;
Dinic dinic;
dinic.init(n);
for(int i = ; i < m; i++){
scanf("%d%d%d",&from,&to,&cap);
dinic.AddEdge(from,to,cap);
}
printf("%d\n",dinic.Maxflow(,n));
}
return ;
}
/**
题目:poj1273 Drainage Ditches
链接:http://poj.org/problem?id=1273
题意:裸的最大流
思路:EdmondsKarp最大流 */
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
const int N = ;
struct Edge{
int from, to, cap, flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct EdmondsKarp
{
int n, m;
vector<Edge>edges;
vector<int>G[N];
int a[N];
int p[N]; void init(int n){
for(int i = ; i<= n; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,));
edges.push_back(Edge(to,from,,));
m = edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
int Maxflow(int s,int t)
{
int flow = ;
for(;;){
memset(a, , sizeof a);
queue<int> Q;
Q.push(s);
a[s] = INF;
while(!Q.empty()){
int x = Q.front(); Q.pop();
for(int i = ; i < G[x].size(); i++){
Edge& e = edges[G[x][i]];
if(!a[e.to]&&e.cap>e.flow){
p[e.to] = G[x][i];
a[e.to] = min(a[x],e.cap-e.flow);
Q.push(e.to);
}
}
if(a[t]) break;
}
if(!a[t]) break;
for(int u = t; u != s; u = edges[p[u]].from){
edges[p[u]].flow += a[t];
edges[p[u]^].flow -= a[t];
}
flow += a[t];
}
return flow;
}
};
int main()
{
int n, m;
while(scanf("%d%d",&m,&n)==)
{
EdmondsKarp ek;
ek.init(n);
int from, to, cap;
for(int i = ; i < m; i++){
scanf("%d%d%d",&from,&to,&cap);
ek.AddEdge(from,to,cap);
}
printf("%d\n",ek.Maxflow(,n));
}
return ;
}

最新文章

  1. WebClient 数据传输
  2. 使用jQuery封装实用函数
  3. Postgresql 取随机数
  4. Android 性能测试
  5. Orchard源码分析(4.4):Orchard.Caching.CacheModule类
  6. Book-编程珠玑-第一章
  7. 忘记Mysql登录密码
  8. 【深入浅出.Net IL】1.一个For循环引发的IL
  9. 树莓派 Linux 剪贴板
  10. Windows不能在本地计算机启动OracleDBConsoleorcl .错误代码2
  11. [转python 父类可以调用子类的方法
  12. JavaScript中国象棋程序(7) - 置换表
  13. php正则匹配
  14. IntelliJ IDEA 2018 破解过程[详细步骤](Mac OS &amp; Windows)
  15. Python module ---- getopt
  16. sqli-labs(十三)(hpp)
  17. django之relacted.py(探秘django的关联field)
  18. Spring MVC 指导文档解读(二)
  19. 使用caffe训练自己的图像数据(未完)
  20. java容器---Comparable &amp; Comparator

热门文章

  1. ORA-12537:TNS连接已关闭
  2. gcc static静态编译选项提示错误:/usr/lib/ld:cannot find -lc
  3. GLEW扩展库【转】
  4. http://blog.csdn.net/pet8766/article/details/8186955
  5. mysql 5.5多实例部署
  6. 【LeetCode】Find Minimum in Rotated Sorted Array 解题报告
  7. IDEA+MAVEN+testNG(reportNG)
  8. 用C++实现文件压缩(1.5)
  9. 算法(第四版)学习笔记之java实现可以动态调整数组大小的栈
  10. Mycat探索之旅(3)----Mycat的全局序列号