Drainage Ditches (HDU - 1532)(最大流)
2024-09-05 04:45:41
题意:有m个点,n条管道,问从1到m最大能够同时通过的水量是多少?
题解:最大流模板题。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 0x3ffffff;
const int maxn = 205;
int gra[maxn][maxn];
int flow[maxn];
int path[maxn];
int n,m;
int bfs(int s, int e)
{
queue<int>q;
int t;
memset(path,-1,sizeof(path));
memset(flow,0,sizeof(flow));
path[s] = 0;
flow[s] = INF;
q.push(s);
while(!q.empty()){
t = q.front();
q.pop();
if(t == e) break;
for(int i = 1; i <= m; i ++)
{
if(i != s && gra[t][i] && path[i] == -1)
{
flow[i] = flow[t] < gra[t][i] ? flow[t] : gra[t][i];
path[i] = t;
q.push(i);
}
}
}
if(path[e] == -1)
return -1;
else return flow[e];
}
int EK(int s, int e){
int maxflow = 0;
int pre, now, step;
while((step = bfs(s,e))!= -1){
maxflow += step;
now = e;
while(now != s){
pre = path[now];
gra[pre][now] -= step;
gra[now][pre] += step;
now = pre;
}
}
return maxflow;
}
int main()
{
int u,v,c;
while(~scanf("%d%d",&n,&m)){
memset(gra,0,sizeof(gra));
for(int i = 1; i <= n; i ++)
{
scanf("%d %d %d", &u,&v,&c);
gra[u][v] += c;
}
int ans = EK(1,m);
printf("%d\n",ans);
}
return 0;
}
最新文章
- 推荐一篇关于java 学习的文章,感觉写的很不错
- vim 配置
- Spring学习总结(三)——Spring实现AOP的多种方式
- python中from module import * 的一个陷阱
- python设计模式之--单例模式
- NDK学习二: NDK目录结构
- 制作、解析带logo的二维码
- Spring+MyBatis多数据源配置实现
- How-to: disable the web-security-check in Chrome for Mac
- android:ToolBar详解(手把手教程)
- JavaScript---闭包和作用域链
- Jquery datatables 重载数据方法
- MySQL默认INFORMATION_SCHEMA,MySQL,TEST三个数据库用途
- BackgroundWorker 后台进程控制窗体label、richtextbook内容刷新
- 如何在程序退出的时候清除activity栈
- LINQ To SQL在N层应用程序中的CUD操作、批量删除、批量更新
- CF368 D - Persistent Bookcase
- 初涉IPC,了解AIDL的工作原理及使用方法
- 报错:Heartbeating to master:7182 failed.
- 【python】Django自定义模板函数