poj1273 Drainage Ditches Dinic最大流

时间:2021-05-03 07:22:42

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 ;
}