poj1273 Drainage Ditches Dinic最大流
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 76000 | Accepted: 29530 |
Description
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
Output
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 ;
}
poj1273 Drainage Ditches Dinic最大流的更多相关文章
- 2018.07.06 POJ1273 Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Description Every time it rains on Farmer J ...
- Drainage Ditches(Dinic最大流)
http://poj.org/problem?id=1273 用Dinic求最大流的模板题,注意会有重边. 邻接矩阵建图 #include<stdio.h> #include<str ...
- poj1273 Drainage Ditches (最大流模板)
http://poj.org/problem?id=1273 Dinic算法 这是一道最大流的经典题 最大流尽量应该用边表,优于邻接矩阵(所以我写了邻接矩阵版的之后又写了个边表) 用了新学的Dinic ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- 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 Drainage Ditches 最大流Dinic
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65146 Accepted: 25112 De ...
- HDU 1532||POJ1273:Drainage Ditches(最大流)
pid=1532">Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- poj-1273 Drainage Ditches(最大流基础题)
题目链接: Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67475 Accepted ...
- poj1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68414 Accepted: 2648 ...
随机推荐
- spark DiskBlockManager
RDD本身presist可以是本地存储,本地存储级别的持久化实现方式如下: DiskBlockManager负责管理和维护block和磁盘存储的映射关系,通过blockId作为文件名称,然后如果是多个 ...
- Jackson使用ObjectManage#readValue传入泛型T的问题
说明:没找到合适的方法,持续关注这个问题 参考: https://stackoverflow.com/questions/11664894/jackson-deserialize-using-gene ...
- Linux查找某个时间点后生成的文件(转)
需要找到某天(例如2017-04-13)以及这之后生成的空文件.那么这个要怎么处理呢?这个当然是用find命令来解决.如下所示, -mtime -5表示查找距现在5*24H内修改过的文件 -type ...
- 解密所有APP运行过程中的内部逻辑(转)
转贴地址:http://www.freebuf.com/tools/54562.html 0×01前言 这年头,apk 全都是加密啊,加壳啊,反调试啊,小伙伴们表示已经不能愉快的玩耍了.静态分析越来越 ...
- WPF附加属性的Set函数不调用的问题
今天写程序的时候用到了附加属性,我是用VS内置的propa的代码段来实现的,代码如下: class Attach { public static bool GetIsEnabled ...
- 在C#中快速查询文件
相信使用过Everything的人都对其超快的搜索速度印象非常深刻,它的主要原理是通过扫描NTFS磁盘的USN Journal读取的文件列表,而不是磁盘目录,由于USN Journal非常小,因此能实 ...
- Linux下打包命令tar
转:http://blog.chinaunix.net/uid-29021161-id-3922752.html Linux下最常用的打包程序是tar,用tar命令打成的包文件通常以.tar结尾 1. ...
- TDiocpCoderTcpServer返回数据记录有条数限制的问题
TDiocpCoderTcpServer返回数据记录有条数限制的问题 在使用TDiocpCoderTcpServer控件返回查询数据的时候,发现当记录条数超过一定数量的时候(比方有人反试图返回30万条 ...
- ubuntu14.04使用samba共享文件
samba是linux中常用的共享文件的软件 ubuntu12.04及以上版本中已经安装了samba 开始配置: samba配置文件: /etc/samba/smb.conf可以修改配置文件来设置sa ...
- 使用Spring进行远程访问与Web服务
1.1. 简介 Spring为各种远程访问技术的集成提供了整合类.Spring使得开发具有远程访问功能的服务变得相当容易,而这些远程访问服务由普通Spring POJO实现.目前,Spring支持 ...