Drainage Ditches

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

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
 
Source
 
题意:  有关输水网络,给出每一段水管的最大输水流量,问整个系统出水的最大流量是多少......
看到这道题,完全惊呆了,一道原生态的网络流问题,而且问题还这么的直白。╮(╯▽╰)╭,但是呵呵,杭电有展现了他机智的一面,说的数据和给的数据不一致,说好的200,然后要到了500,然后果断的献上了好几个wa。
看了一下discuss里的僵尸版的提示,又去修改一下代码....O(∩_∩)O~呵~呵,感觉不会再爱了,居然还有重边,我真的是....,优秀改了一下矩阵的构图,又大方的献了几个wa....
最后终于AC...当然对于这样的一个水体(带坑),也还是要高兴高兴....
首先献上一个EK算法的代码:(不知道什么时候学的,貌似是算法竞赛里刘奴佳介绍的)..时间复杂度有点小高...但是数据很弱居然oms过了,我又.......感受到了这个世界的恶意啊╮(╯▽╰)╭
  0ms
 #include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=;
int map[maxn][maxn];
int dist[maxn];
int n,m;
int bfs(int st,int en){
int t;
queue<int>q;
memset(dist,-,sizeof(int)*(m+));
q.push(st);
dist[st]=;
while(!q.empty()){
t=q.front();
q.pop();
for(int i=;i<=m;i++){
if(map[t][i]>&&dist[i]<){
dist[i]=dist[t]+;
q.push(i);
}
}
}
if(dist[en]>) return ;
return ;
}
int dfs(int st,int en,int flow){
int tem=;
if(st==en||flow==)return flow;
for(int i=;i<=m;i++)
{
if(dist[i]==dist[st]+&&map[st][i]>&&(tem=dfs(i,en,min(map[st][i],flow))))
{
map[st][i]-=tem;
map[i][st]+=tem;
return tem;
}
}
return ;
}
void Dinic(int st,int en)
{
int ans=;
while(bfs(st,en))
ans+=dfs(st,en,inf);
printf("%d\n",ans);
}
int main()
{
int i,a,b,c;
while(scanf("%d%d",&n,&m)!=EOF){
memset(map,,sizeof(map));
for(i=;i<=n;i++){
scanf("%d%d%d",&a,&b,&c);
map[a][b]+=c;
}
Dinic(,m);
}
return ;
}

优化优化,用一下邻接表做...

代码:内存立马减少到了 276k

代码:

 #include<stdio.h>
#include<string.h>
#include<queue>
#define ma 502
#define inf 0x3f3f3f3f
using namespace std;
int head[ma];
struct node
{
int to;
int w;
int next;
};
node mat[ma];
int dist[ma];
int pos,n,m;
int min(int a,int b){
return a>b?b:a;
}
void add(int a,int b,int flow){
mat[pos].to=b;
mat[pos].w=flow;
mat[pos].next=head[a];
head[a]=pos++;
} bool bfs(int st,int to){
memset(dist,-,sizeof(int)*(n+));
queue<int> q;
q.push(st);
dist[st]=;
int t;
while(!q.empty()){
t=q.front();
q.pop();
for(int i=head[t];~i;i=mat[i].next){
if(dist[mat[i].to]<&&mat[i].w>){
dist[mat[i].to]=dist[t]+;
if(mat[i].to==to) return ;
q.push(mat[i].to);
}
}
}
return ;
} int dfs(int st,int to,int flow){ int tem=;
if(st==to||flow==) return flow;
for(int i=head[st];~i;i=mat[i].next){
if(mat[i].w>&&dist[mat[i].to]==dist[st]+&&(tem=dfs(mat[i].to,to,min(flow,mat[i].w))))
{
mat[i].w-=tem;
mat[i^].w+=tem;
return tem;
}
}
return ;
}
int Dinic(int st,int to){
int ans=;
while(bfs(st,to))
ans+=dfs(st,to,inf);
return ans;
} int main()
{
int a,b,c;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(head,-,sizeof(int)*(n+));
pos=;
while(m--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c); //单向边
add(b,a,);
}
printf("%d\n",Dinic(,n));
}
return ;
}
 

最新文章

  1. MySQL时间戳相互转换
  2. Windows 2012 安装 .net framework 3.5
  3. UITableView + UISearchBar 实现搜索功能
  4. bnu 4359 无爱编号(规律)
  5. PL/SQL常见设置--Kevin的专栏
  6. JAVA之File类创建对象构造函数传参数需要注意的几点
  7. [Android学习笔记]设置Activity方向
  8. C++生成dump文件
  9. [Luogu2617]Dynamic Rankings(整体二分)
  10. centos7 Docker私有仓库搭建及删除镜像
  11. javascript数组的实例属性(方法)
  12. 【题解】Luogu SP3267 DQUERY - D-query
  13. HDU 1524 树上无环博弈 暴力SG
  14. vc++加载透明png图片方法-GDI+和CImage两种
  15. photoshop切图介绍 &amp;&amp; photoshop下载与破解
  16. kindle 电子书去除DRM
  17. ubuntu下code::blocks设置运行窗口为gnome命令行
  18. Visual Studio 2005 自带单元测试
  19. 如何让自己的exe程序开机自启动
  20. Buffer Pool--内存总结2

热门文章

  1. 借助Nodejs在服务端使用jQuery采集17173游戏排行信息
  2. Linux的查找命令
  3. CSS笔记(六)链接
  4. JAVA中对Cookie的操作
  5. Ext.net 异常统一管理,铥掉可恶的 Request Failure
  6. python操作postgresql数据库
  7. .net连接access操作类
  8. html的空格和换行显示【摘自网络】
  9. Android LayoutInflater 动态地添加删除View
  10. HTML的&lt;body&gt;标签详解与HTML常用的控制标记