POJ - 1273 Drainage Ditches(最大流)
题目链接:点击查看
题目大意:现在一共有n个节点,需要修建m条水渠将池塘的水引入小溪,点1代表池塘,点n代表小溪,每条水渠都有一定的排水能力,问最大的排水效率是多少
题目分析:网络流的模板题,直接套板子就行了
代码:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=210;struct Edge
{int to,w,next;
}edge[N*2];//边数int head[N],cnt;void addedge(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向边边权设置为0edge[cnt].next=head[v];head[v]=cnt++;
}int d[N],now[N*N];//深度 当前弧优化bool bfs(int s,int t)//寻找增广路
{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;
}int dinic(int x,int t,int flow)//更新答案
{if(x==t)return flow;int rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(w&&d[v]==d[x]+1){int k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;
}void init()
{memset(head,-1,sizeof(head));cnt=0;
}int main()
{
// freopen("input.txt","r",stdin);
// ios::sync_with_stdio(false);int n,m;while(scanf("%d%d",&m,&n)!=EOF){init();while(m--){int x,y,w;scanf("%d%d%d",&x,&y,&w);addedge(x,y,w);}int ans=0;int flow;while(bfs(1,n))while(flow=dinic(1,n,inf))ans+=flow;printf("%d\n",ans);}return 0;
}
POJ - 1273 Drainage Ditches(最大流)相关推荐
- POJ 1273 Drainage Ditches 最大流
很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> ...
- 网络流--最大流--POJ 1273 Drainage Ditches
链接 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clov ...
- [POJ 1273]Drainage Ditches
[问题描述] Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch ...
- POJ 1273 Drainage Ditches
网络流. 题意非常easy.给出单向边,容量. 找最大流.注意重边要加起来.g[u][v].c+=c; 第一次写网络流. 也是第一个网络流的题. 看了两天,理解了之后就唰唰唰的写出来了. 大概可能是E ...
- [USACO4.2] 草地排水 Drainage Ditches (最大流)
题目背景 在农夫约翰的农场上,每逢下雨,贝茜最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段时间.因此,农夫约翰修建了一套排水系统来使贝茜的草地免除被大水淹没 ...
- POJ 1273 Dinic
题意 传送门 POJ 1273 Drainage Ditches 题解 最大流模板题,使用 DinicDinicDinic 算法求解. #include <algorithm> #incl ...
- poj1273 Drainage Ditches
蒟蒻的blog POJ 1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Memory Limit: 10000K Total ...
- USACO Section 4.2 Drainage Ditches(最大流)
最大流问题.ISAP算法.注意可能会有重边,不过我用的数据结构支持重边.距离d我直接初始化为0,也可以用BFS逆向找一次. -------------------------------------- ...
- Drainage Ditches POJ1273
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 93263 Accepted: 36174 试题链接 文章目录 Descripti ...
最新文章
- 算法模板——二分图匹配
- python输入逗号分隔值文件_如何在Python(Pygame)中显示逗号分隔值(.txt)文件中的前10个高分...
- .net 连接mysql的字符串_asp.net连接数据库字符串
- 到底是Java好还是Python好?
- 华为产品技术学习笔记之路由原理(二)
- SAP Spartacus login 超链接和 login form 的区别
- 在熟练使用2B铅笔前,请不要打开Axure
- java servlet+oracle 新手可看
- 我的nginx iis 负载均衡学习(环境搭建)
- 厦门大学c语言模拟考试题,厦门大学《C语言》模拟试卷(10级).doc
- text html mime img,使用Python的email.mime.multipart发送HTML邮件时命...
- ios上传图片文件到服务器,iOS 图片以文件形式上传到服务器
- 6个高级Python技巧
- 2.Rails程序框架
- BP神经网络参数设置及实例
- pytorch BCELoss()、KLDivLoss()的参数 及 “对于size_average、reduce、reduction参数的研究”
- unity2018设置雾在哪_【zSpace实操】Unity2018发布设置
- 视频教程-2020软考数据库系统工程师-基础知识培训视频-软考
- jsoniter与原生json对比
- 基于JavaWeb的小说阅读网站设计与实现 毕业论文+答辩PPT+项目源码及数据库文件