【luogu P2936 [USACO09JAN]全流Total Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P2936
菜
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000;
const int inf = 1e9;
int n, m, s, t, maxflow, deep[maxn];
struct edge{
int next, to, len;
}e[maxn<<2];
int head[maxn], cnt = -1, cur[maxn];
queue<int> q;
void add(int u, int v, int w, int flag)
{
e[++cnt].next = head[u];
e[cnt].to = v;
if(flag) e[cnt].len = w;
head[u] = cnt;
}
bool bfs(int s, int t)
{
memset(deep, 0x7f, sizeof(deep));
while(!q.empty()) q.pop();
for(int i = 1; i <= n; i++) cur[i] = head[i];
q.push(s); deep[s] = 0;
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] > inf && e[i].len)
{
deep[e[i].to] = deep[now] + 1;
q.push(e[i].to);
}
}
}
if(deep[t] < inf) return true;
else return false;
}
int dfs(int now, int t, int limit)
{
if(!limit || now == t) return limit;
int flow = 0, f;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] == deep[now] + 1 && (f = dfs(e[i].to, t, min(e[i].len, limit))))
{
flow += f;
limit -= f;
e[i].len -= f;
e[i^1].len += f;
if(!limit) break;
}
}
return flow;
}
void Dinic(int s, int t)
{
while(bfs(s, t))
maxflow += dfs(s, t, inf);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d",&m);
s = 1, t = 26;
for(int i = 1; i <= m; i++)
{
char a, b; int u, v, w;
cin>>a>>b>>w;
u = a-'A'+1;
v = b-'A'+1;
//cout<<u<<" "<<v<<endl;
add(u, v, w, 1);
add(v, u, w, 0);
}
Dinic(s, t);
printf("%d\n",maxflow);
return 0;
}
【luogu P2936 [USACO09JAN]全流Total Flow】 题解的更多相关文章
- 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)
P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...
- 洛谷——P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- 洛谷 P2936 [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- [USACO09JAN]全流Total Flow
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]
题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...
- luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)
链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...
- 【luogu P3128 [USACO15DEC]最大流Max Flow】 题解
题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> ...
- luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)
题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...
随机推荐
- Spark 1.6.2 + Beam 2.0.0读取Mongodb数据进行相应逻辑处理
不多说,直接上干货! http://blog.csdn.net/jianglushou9763/article/details/73332805 如果需要 APACHE BEAM2.0.0版本如何支持 ...
- React.js 小书 Lesson16 - 实战分析:评论功能(三)
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson16 转载请注明出处,保留原文链接和作者信息. 接下来的代码比较顺理成章了.修改 Commen ...
- React.js 小书 Lesson8 - 组件的组合、嵌套和组件树
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson8 转载请注明出处,保留原文链接和作者信息. 继续拓展前面的例子,现在我们已经有了 Heade ...
- vim脚本语言
转自:http://man.chinaunix.net/newsoft/vi/doc/usr_41.html#usr_41.txt Vim 脚本语言在很多地方用到,包括 vimrc 文件, 语法文件, ...
- Python快速入门_1
注释 # 用#号字符开头注释单行 """ 三个引号可以注释多行 三个引号可以注释多行 三个引号可以注释多行 """ 原始数据类型和运算符 ( ...
- 对while;do while;for三种循环语句的理解与区分。
while:先判断表达式的值,在表达式值为真的情况下执行循环语句,直到表达式值为假结束循环: while(循环条件) { 循环体. } do-while:先执行循环体语句一次,再判别表达式的值,在表达 ...
- python爬取英语学习资料并发送邮件
新建发送邮件类 import smtplib from email.mime.text import MIMEText from email.header import Header class Se ...
- Java进程内缓存
今天和同事聊到了缓存,在Java中实现进程缓存.这里主要思想是,用一个map做缓存.缓存有个生存时间,过期就删除缓存.这里可以考虑两种删除策略,一种是起一个线程,定期删除过期的key.第二个是,剔除模 ...
- oracle学习篇七:更新操作、事务处理
----------------1.数据库更新操作----------------------------- select * from tab;--查询表 drop table siebel_use ...
- linux定时任务crontab的使用
crond进程: crond是linux下用来周期性地执行某种任务的一个守护进程,安装操作系统默认会安装此服务工具,并且会自动启动crond进程. 设置定时任务过程: 1. 创建任务文件(.sh) [ ...