Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7845 Accepted Submission(s): 3659
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.
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
题意:给出n个河流,m个点,以及每个河流的流量,求从1到m点的最大流量。
分析:最裸的网络流题目
网络流算法ford-fulkerson讲解:
http://blog.csdn.net/y990041769/article/details/21026445
这个题目数据量小,邻接表和邻接矩阵都可以过。当然最好是用邻接表:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
const int N = 1100;
const int INF = 0x3f3f3f3f;
struct Node
{
int to;//终点
int cap; //容量
int rev; //反向边
};
vector<Node> v[N];
bool used[N];
void add_Node(int from,int to,int cap) //重边情况不影响
{
v[from].push_back((Node){to,cap,v[to].size()});
v[to].push_back((Node){from,0,v[from].size()-1});
}
int dfs(int s,int t,int f)
{
if(s==t)
return f;
used[s]=true;
for(int i=0;i<v[s].size();i++)
{
Node &tmp = v[s][i]; //注意
if(used[tmp.to]==false && tmp.cap>0)
{
int d=dfs(tmp.to,t,min(f,tmp.cap));
if(d>0)
{
tmp.cap-=d;
v[tmp.to][tmp.rev].cap+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0;
for(;;){
memset(used,false,sizeof(used));
int f=dfs(s,t,INF);
if(f==0)
return flow;
flow+=f;
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(v,0,sizeof(v));
for(int i=0;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add_Node(x,y,z);
}
printf("%d\n",max_flow(1,m));
}
}
邻接矩阵BFS
#include <cstdio>
#include <vector>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 300;
const int MAX = 0x3f3f3f3f;
int map[N][N];
int flow[N][N];
int a[N],p[N];
int Ford_fulkerson(int s,int t)
{
queue<int> qq;
memset(flow,0,sizeof(flow));
int f=0,u,v;
while(1)
{
memset(a,0,sizeof(a));
a[s]=MAX;
qq.push(s);
while(!qq.empty())
{
u=qq.front();qq.pop();
for(v=1;v<=t;v++)
{
if(!a[v]&&map[u][v]>flow[u][v])//找到新结点v
{
p[v]=u;qq.push(v);//记录v的父亲,并加入FIFO队列
a[v]=a[u]<map[u][v]-flow[u][v]?a[u]:map[u][v]-flow[u][v];//a[v]为s-v路径上的最小流量
}
}
}
if(a[t]==0)
return f;
for(int i=t;i!=s;i=p[i])
{
flow[i][p[i]]-=a[t];
flow[p[i]][i]+=a[t];
}
f+=a[t];
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
memset(map,0,sizeof(map));
for(int i=0;i<n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
map[x][y]+=z;
}
printf("%d\n",Ford_fulkerson(1,m));
}
}
dinci算法邻接表:
#include <cstdio>
#include <vector>
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 300;
const int MAX = 0x3f3f3f3f;
#define Min(a,b) a>b?b:a
int dis[N];
int map[N][N];
int m,n;
int BFS() //构建层次网络
{
memset(dis,-1,sizeof(dis));
dis[1]=0;
queue<int> v;
v.push(1);
while(!v.empty())
{
int x=v.front();v.pop();
for(int i=1;i<=n;i++)
{
if(dis[i]<0 && map[x][i]>0)
{
dis[i]=dis[x]+1;
v.push(i);
}
}
}
if(dis[n]>0)
return 1;
else
return 0;
}
int DFS(int x,int low)//Low是源点到现在最窄的(剩余流量最小)的边的剩余流量
{
int i,a=0;
if (x==n)
return low;//是汇点
for(i=1;i<=n;i++)
if (map[x][i] >0 //联通
&& dis[i]==dis[x]+1 //是分层图的下一层
&&(a=DFS(i,min(low,map[x][i]))))//能到汇点(a <> 0)
{
map[x][i] -= a;
map[i][x] += a;
return a;
}
return 0;
}
int main()
{
while(~scanf("%d%d",&m,&n))
{
memset(map,0,sizeof(map));
for(int i=0;i<m;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
map[x][y]+=z;
}
int ans=0,tmp;
while(BFS())
{
while(tmp=DFS(1,0x3f3f3f3f))
ans+=tmp;
}
printf("%d\n",ans);
}
return 0;
}