Drainage Ditches(Dinic最大流)
http://poj.org/problem?id=1273
用Dinic求最大流的模板题,注意会有重边。
邻接矩阵建图
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int flow[maxn][maxn];//残量
int m,n;
int dis[maxn];
int bfs()//按层数“建”图,就是对每层的点用dis标记它到源点的层数
{
queue<int>que;
memset(dis,-,sizeof(dis));
while(!que.empty())
que.pop();
dis[] = ;
que.push(); while(!que.empty())
{
int u = que.front();
que.pop(); for(int i = ; i <= n; i++)
{
if(flow[u][i] > && dis[i] < )
{
dis[i] = dis[u]+;
que.push(i);
}
}
} if(dis[n] > )
return ;
else return ;
}
//dfs寻找路径上的最小流量
int dfs(int s, int mf)
{
int a;
if(s == n)
return mf;
for(int i = ; i <= n; i++)
{
if(dis[i] == dis[s] + && flow[s][i] > && (a = dfs(i,min(mf,flow[s][i]))))
{
flow[s][i] -= a;
flow[i][s] += a;
return a;
}
}
return ;
} int main()
{
while(~scanf("%d %d",&m,&n))
{
int u,v,w;
memset(flow,,sizeof(flow));
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
flow[u][v] += w;
}
int ans = ,res;
while(bfs())//bfs寻找源点到汇点是否有路
{
res = dfs(,INF);
if(res > )
ans += res;
}
printf("%d\n",ans);
}
return ; }
邻接表建图
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
int m,n,cnt;
struct node
{
int u,v,w;
int next;
}edge[maxn];
int p[maxn];
int dis[maxn];
void add(int u, int v, int w)
{ edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = p[u];
p[u] = cnt++; edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].w = ;
edge[cnt].next = p[v];
p[v] = cnt++;
} int bfs()
{
queue<int> que;
while(!que.empty())
que.pop();
memset(dis,-,sizeof(dis));
dis[] = ;
que.push(); while(!que.empty())
{
int u = que.front();
que.pop(); for(int i = p[u]; i != -; i = edge[i].next)
{
if(edge[i].w > && dis[ edge[i].v ] < )
{
dis[ edge[i].v ] = dis[u] + ;
que.push(edge[i].v);
}
}
}
if(dis[n] > )
return ;
else return ;
} int dfs(int s, int mf)
{
if(s == n)
return mf;
int a;
int tf = ;
for(int i = p[s]; i != -; i = edge[i].next)
{
int v = edge[i].v;
int w = edge[i].w;
if(dis[v] == dis[s] + && w > && (a = dfs(v,min(mf,w))))
{
edge[i].w -= a;
edge[i^].w += a;
return a;
}
}
if(!tf)//若找不到小流,从这个点到不了汇点,把这个点从分层图删去
dis[s] = -;
return tf;
} int main()
{
while(~scanf("%d %d",&m,&n))
{
int u,v,w;
cnt = ;
memset(p,-,sizeof(p));
for(int i = ; i < m; i++)
{
scanf("%d %d %d",&u,&v,&w);
add(u,v,w);
}
int ans = ;
int res;
while(bfs())
{
res = dfs(,INF);
if(res > )
ans += res;
}
printf("%d\n",ans);
} return ;
}
Drainage Ditches(Dinic最大流)的更多相关文章
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- POJ1273&&Hdu1532 Drainage Ditches(最大流dinic) 2017-02-11 16:28 54人阅读 评论(0) 收藏
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 1273 (nyoj 323) Drainage Ditches : 最大流
点击打开链接 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49648 Accepte ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
- poj 1273 && hdu 1532 Drainage Ditches (网络最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53640 Accepted: 2044 ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- POJ 1273 Drainage Ditches -dinic
dinic版本 感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了 Description Every time it rains on Farmer John's fields, ...
- poj 1273 Drainage Ditches【最大流入门】
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63924 Accepted: 2467 ...
随机推荐
- MyEclipse常见配置及调试
常见配置 1.配置workspace ----- 建议不要采用含有空格和中文目录 所有代码保存workspace空间中2.新建工程时,设置工程需要jre环境MyEclipse提供多种内置layout ...
- TransactionScope事务
一个错误的理解就是Complete()方法是提交事务的,这是错误的,事实上,它的作用的表示本事务完成,它一般放在try{}的结尾处,不用判断前台操作是否成功,如果不成功,它会自己回滚. #region ...
- ASP.NET 设计模式中依赖倒置原则
依赖倒置原则 A.高层次的模块不应该依赖于低层次的模块,他们都应该依赖于抽象. B.抽象不应该依赖于具体,具体应该依赖于抽象. 依赖倒置原则 A.高层次的模块不应该依赖于低层次的模块,他们都应该依赖于 ...
- 那天有个小孩跟我说LINQ(六)转载
2 LINQ TO SQL完结(代码下载) 我们还是接着上次那个简单的销售的业务数据库为例子,打开上次那个例子linq_Ch5 2.1 当数据库中的表建立了主外键 ①根据主键获取子表信息 ...
- JDBC对sql server的操作
1.过程: 1>注册驱动器类:Class.forName() 2>连接数据库: String url = "jdbc:sqlserver:// ...
- CSS控制长文本内容显示(截取的地方用省略号代替)
自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,下面介绍的是CSS如何实现处理的方法. 现实中经常出现一些内容比较长的文本,为了使整体布局美观,需要将文本内容控制在一行 ...
- ASP.NET MVC5总结(三)登陆中常用技术解析之session与cookie
1.session机制 session机制是在服务器端保持状态的方案,在做系统登陆时,我们往往会用到session来存储一些用户登录的重要信息,而这些信息是不能存在cookie中的. 当访问量增多时, ...
- c++ primer复习(二)
1 悬垂else来自于else语句搭配的if语句的二义性 2 理解switch代码: switch(c) { case 'a': a++; case 'b': b++; default: x++; } ...
- Linux命令——监视相关
1.netstat 用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...
- javaScript & jquery完美判断图片是否加载完毕
好久没写东西了,正好最近因为工作需要,写了一个瀑布流异步加载的程序. 今天就不谈瀑布流,来谈一下关于load的问题. ----------------------------------------- ...