【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 ...
随机推荐
- TOJ 4393 Game
描述 Bob always plays game with Alice.Today,they are playing a game on a tree.Alice has m1 stones,Bob ...
- topN问题
topN问题:给出一个数组,找出前N个最大的元素. topN问题可以用分治法解决,这个问题与快速排序类似,快速排序是用一个数对数组进行划分,topN问题则不需完成排序,只需划分出前n个最大的数字即可. ...
- nyoj 546——Divideing Jewels——————【dp、多重背包板子题】
Divideing Jewels 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Mary and Rose own a collection of jewells. ...
- Excle 导入DataSet
using System.Data.OleDb;using System.Data; public void ReadExcelFiless() { //strin ...
- axios请求报Uncaught (in promise) Error: Request failed with status code 404
使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...
- Maven入门之简介与安装
一.Maven简介 1.什么是Maven? Maven是一个项目管理工具和集成编译工具,它主要包含如下内容: –一个项目对象模型(Project Object Model), –一组标准集合, –一个 ...
- Python3.4 获取百度网页源码并保存在本地文件中
最近学习python 版本 3.4 抓取网页源码并且保存在本地文件中 import urllib.request url='http://www.baidu.com' #上面的url一定要写明确,如果 ...
- Spring Chapter4 WebSocket 胡乱翻译 (一)
4. WebSocket 包含了Servlet stack,原生WebSocket交互,通过SockJS模拟,并且通过STOMP在WebSocket之上订阅.发布消息. 4.1 简介 不扯了,看到这个 ...
- 深入理解 Java 内存模型(转载)
摘要: 原创出处 http://www.54tianzhisheng.cn/2018/02/28/Java-Memory-Model/ 「zhisheng」欢迎转载,保留摘要,谢谢! 0. 前提 &l ...
- Linux 启动SVN服务
#使用默认端口3690启动svn服务svnserve -d -r /home/svndata # 如果出现#svnserve: Can't bind server socket: Address al ...