HD1532Drainage Ditches(最大流模板裸题 + 邻接表)

Drainage Ditches

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13273    Accepted Submission(s): 6288

Problem 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
 
 
第一道最大流题目!
 
 #include <iostream>
#include <queue>
#include<string.h>
using namespace std;
#define arraysize 201
int maxData = 0x7fffffff;
int capacity[arraysize][arraysize]; //记录残留网络的容量
int flow[arraysize]; //标记从源点到当前节点实际还剩多少流量可用
int pre[arraysize]; //标记在这条路径上当前节点的前驱,同时标记该节点是否在队列中
int n,m;
queue<int> myqueue;
int BFS(int src,int des)
{
int i,j;
while(!myqueue.empty()) //队列清空
myqueue.pop();
for(i=;i<m+;++i)
{
pre[i]=-;
}
pre[src]=;
flow[src]= maxData;
myqueue.push(src);
while(!myqueue.empty())
{
int index = myqueue.front();
myqueue.pop();
if(index == des) //找到了增广路径
break;
for(i=;i<m+;++i)
{
if(i!=src && capacity[index][i]> && pre[i]==-)
{
pre[i] = index; //记录前驱
flow[i] = min(capacity[index][i],flow[index]); //关键:迭代的找到增量
myqueue.push(i);
}
}
}
if(pre[des]==-) //残留图中不再存在增广路径
return -;
else
return flow[des];
}
int maxFlow(int src,int des)
{
int increasement= ;
int sumflow = ;
while((increasement=BFS(src,des))!=-)
{
int k = des; //利用前驱寻找路径
while(k!=src)
{
int last = pre[k];
capacity[last][k] -= increasement; //改变正向边的容量
capacity[k][last] += increasement; //改变反向边的容量
k = last;
}
sumflow += increasement;
}
return sumflow;
}
int main()
{
int i,j;
int start,end,ci;
while(cin>>n>>m)
{
memset(capacity,,sizeof(capacity));
memset(flow,,sizeof(flow));
for(i=;i<n;++i)
{
cin>>start>>end>>ci;
if(start == end) //考虑起点终点相同的情况
continue;
capacity[start][end] +=ci; //此处注意可能出现多条同一起点终点的情况
}
cout<<maxFlow(,m)<<endl;
}
return ;
}
刘汝佳书上给的邻接表算法
 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#include <string.h>
using namespace std;
const int MAX = ;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){};
};
int n,m;
vector<Edge> edge;
vector<int> g[MAX];
int a[MAX],p[MAX];
void init()
{
for(int i = ; i <= m; i++)
g[i].clear();
edge.clear();
}
int Maxflow(int s,int t)
{
int flow = ;
while(true)
{
memset(a,,sizeof(a));
queue<int> q;
q.push(s);
a[s] = INF;
while(q.size())
{
int x = q.front();
q.pop();
int len = g[x].size();
for(int i = ; i < len; i++)
{
Edge e = edge[ 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 i = t; i != s; i = edge[ p[i] ].from)
{
edge[ p[i] ].flow += a[t];
edge[ p[i] ^ ].flow -= a[t];
}
flow += a[t];
}
return flow;
}
int main()
{
while(scanf("%d%d", &n,&m) != EOF)
{
int s,e,v,t;
init();
for(int i = ; i <= n; i++)
{
scanf("%d%d%d",&s,&e,&v);
edge.push_back(Edge(s,e,v,));
edge.push_back(Edge(e,s,,));
t = edge.size();
g[s].push_back(t - );
g[e].push_back(t - );
} printf("%d\n",Maxflow(,m));
}
}
上一篇:Python数据抓取技术与实战 pdf


下一篇:jQuery中实现自定义方法的扩展