您的位置:首页 > 大数据 > 人工智能

POJ 1273 Drainage Ditches【最大流】

2015-05-25 20:33 405 查看
题意:给出起点是一个池塘,M条沟渠,给出这M条沟渠的最大流量,再给出终点是一条河流,问从起点通过沟渠最多能够排多少水到河流里面去

看的紫书的最大流,还不是很理解,照着敲了一遍

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;

typedef long long LL;
const int INF = (1<<30)-1;
const int mod=1000000007;
const int maxn=10005;
int n,m,M;

struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f) :from(u),to(v),cap(c),flow(f) {}
};

vector<Edge> edges;//存边
vector<int> G[maxn];//邻接表
int a[maxn];//到起点i的可改进量
int p[maxn];//最短 路树上p的入弧编号

void init(){
for(int i=0;i<n;i++) G[i].clear();
edges.clear();
}

void addedges(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));//反向弧
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}

int Maxflow(int s,int t){
int flow=0;
for(;;){
memset(a,0,sizeof(a));
queue<int> q;
q.push(s);
a[s]=INF;
while(!q.empty()){
int x=q.front();q.pop();
for(int i = 0;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]^1].flow-=a[t];
}
flow+=a[t];
}
return flow;
}

int main(){
while(scanf("%d %d",&M,&n)!=EOF){
init();
for(int i=0;i<M;i++){
int u,v,c;
scanf("%d %d %d",&u,&v,&c);
u--;v--;
addedges(u,v,c);
}
printf("%d\n",Maxflow(0,n-1));
}
return 0;
}


View Code

寒假的cf就遇到过最大流的题目,当时不会而且还没有学

现在又遇到了,不能再这样了,先学一点点先吧-----------------------

gooooooooooooooo------------

加油====================
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: