链接:http://poj.org/problem?id=1273
题意:农夫的农场被水淹了,他建了一些排水沟来排水,最终把这些水排到小河里,现有n个点,节点1~n-1为池塘,水从1开始流,n为小河。然后有m条排水沟,每条排水沟告诉起点、终点、最大水流速度,现在求这个排水系统的最大排水速度。
网络最大流裸题,dinic模板
#include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cctype> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<stack> #include<ctime> #include<cstdlib> #include<functional> #include<cmath> using namespace std; #define PI acos(-1.0) #define MAXN 100100 #define eps 1e-7 #define INF 0x7FFFFFFF #define seed 131 #define ll long long #define ull unsigned ll #define lson l,m,rt<<1 #define rson r,m+1,rt<<1|1 struct node{ int u,w,next; }edge[5000]; int head[2100],vis[2100],dist[2100]; int n,m,src,sink,cnt; void add_edge(int a,int b,int c){ edge[cnt].u = b; edge[cnt].w = c; edge[cnt].next = head[a]; head[a] = cnt++; } void bfs(){ int i, j; memset(dist,0,sizeof(dist)); queue<int>q; vis[src] = 1; q.push(src); while(!q.empty()){ int u = q.front(); q.pop(); for(i=head[u];i!=-1;i=edge[i].next){ if(!vis[edge[i].u]&&edge[i].w){ q.push(edge[i].u); dist[edge[i].u] = dist[u] + 1; vis[edge[i].u] = 1; } } } } int dfs(int u,int delta){ int i,j; if(u==sink) return delta; else{ int ret = 0; for(i=head[u];i!=-1&δi=edge[i].next){ if(edge[i].w&&dist[edge[i].u]==dist[u]+1){ int dd = dfs(edge[i].u,min(edge[i].w,delta)); edge[i].w -= dd; edge[i^1].w += dd; delta -= dd; ret += dd; } } return ret; } } int maxflow(){ int ret = 0; while(1){ memset(vis,0,sizeof(vis)); bfs(); if(!vis[sink]) break; ret += dfs(src,INF); } return ret; } int main(){ int i,j; int a,b,x; while(scanf("%d%d",&m,&n)!=EOF){ memset(head,-1,sizeof(head)); int cnt = 0; src = 1; sink = n; for(i=0;i<m;i++){ scanf("%d%d%d",&a,&b,&x); add_edge(a,b,x); add_edge(b,a,0); } int ans = maxflow(); printf("%d\n",ans); } return 0; }
POJ--1273--Drainage Ditches【Dinic】网络最大流
时间: 2024-11-05 19:38:37