POJ 1273 Drainage Ditches【图论,网络流】

就是普通的网络流问题,想试试新学的dinic算法,这个算法暑假就开始看国家集训队论文了,之前一直都只用没效率的EK算法,真正学会这个算法还是开学后白书上的描述:dinic算法就是不断用BFS构建层次图然后用DFS寻找增广。然后就是一下午的WA ,除了第一次调dinic的问题外,这道题竟然有多组数据!!!看discuss里好像还有重边的问题,可我用的邻接表有效避免了这个问题~~

#include <iostream>

#include <cstdio>

#include<string.h>

#define inf 0x3f3f3f3f

using namespace std;

const int maxn=600;

inth=0,dist[maxn]={0},nex[maxn*4+20]={0},root[maxn]={0},point[maxn*4+20]={0},flow[maxn*4+20]={0};

int visit[maxn]={0},m;

int min(int a,int b)

{

int ret=a<b ? a : b;

return ret;

}

void add(int x,int y,int c)

{

nex[++h]=root[x];

point[h]=y;

flow[h]=c;

root[x]=h;

}

void bfs(int src)

{

memset(visit,0,sizeof(visit));

memset(dist,0,sizeof(dist));

int l=0,r=1,que[50000]={0};

que[++l]=src;

visit[src]=1;

while(l<=r)

{

int u=que[l++];

for(int i=root[u];i!=0;i=nex[i])

{

if (flow[i]!=0 && visit[point[i]]==0)

{

que[++r]=point[i];

dist[point[i]]=dist[u]+1;

visit[point[i]]=1;

}

}

}

}

int dfs(int u,int d)

{

if(u==m)return d;

int ret=0;

for(int i=root[u];i!=0 && d;i=nex[i])

{

if (flow[i]!=0 && dist[point[i]]==dist[u]+1)

{

int dd=dfs(point[i],min(d,flow[i]));

flow[i]-=dd;

flow[((i-1) ^ 1)+1]+=dd;//这个构造最赞~~!!

d-=dd;

ret+=dd;

}

}

return ret;

}

int main()

{

int x,y,c,ret;

int n;

while(scanf("%d%d",&n,&m)!=EOF)

{

memset(root,0,sizeof(root));

memset(nex,0,sizeof(nex));

for(int i=1;i<=n;i++)

{

scanf("%d%d%d",&x,&y,&c);

add(x,y,c);

add(y,x,0);

}

ret=0;

while (1)//dinic

{

bfs(1);

if (visit[m]==0)break;

ret+=dfs(1,inf);

}

printf("%d\n",ret);

}

return 0;

}

时间: 2024-10-15 08:33:15

POJ 1273 Drainage Ditches【图论,网络流】的相关文章

POJ 1273 Drainage Ditches(网络流,最大流)

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

POJ 1273 Drainage Ditches(网络流模板)

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

POJ 1273 Drainage Ditches(网络流dinic算法模板)

POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdio.h> #include <algorithm> #include <queue> #include <string.h> /* POJ 1273 dinic算法模板 边是有向的,而且存在重边,且这里重边不是取MAX,而是累加和 */ using namespace

poj 1273 Drainage Ditches 基础网络流

1 #include <iostream> 2 #include <queue> 3 using namespace std; 4 int G[300][300]; 5 int Prev[300]; //路径上每个节点的前驱节点 6 bool Visited[300]; 7 int n,m; //m是顶点数目,顶点编号从1开始 1是源,m是汇, n是 边数 8 9 int Augment() 10 { 11 int v; 12 int i; 13 deque<int>

POJ 1273 Drainage Ditches(网络流 最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55893   Accepted: 21449 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

POJ 1273 Drainage Ditches 网络流基础

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

POJ 1273 Drainage Ditches(初识网络流)

开始研究网络流了,看了两个晚上吧,今天总算动手实践一下,有了更深的理解 总结一下:在最大流中,容量与实际流量满足3点: 1.实际流量<=容量 2.任意两点之间   : 流量(a->b)==流量(b->a) 3.流量守恒原则   :从s流出的流量 == t流入的流量 一.为什么叫增广路,因为在所有的流量网络中,会存在一个残量,所以在整个残量网络中,找到一个最小值,加到所有的流量线路里,便叫增广. 二.为什么要修改反向流量,因为在更新流量网时,当前选择的并不一定就是最优解,比如u->v

POJ 1273 Drainage Ditches (网络最大流)

http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55235   Accepted: 21104 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

hdu 1532 poj 1273 Drainage Ditches (最大流)

Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 55276   Accepted: 21122 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