题目链接: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】 题解的更多相关文章

  1. 2018.07.06 洛谷P2936 [USACO09JAN]全流Total Flow(最大流)

    P2936 [USACO09JAN]全流Total Flow 题目描述 Farmer John always wants his cows to have enough water and thus ...

  2. 洛谷——P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  3. 洛谷 P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  4. 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 < ...

  5. [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  6. P2936(BZOJ3396) [USACO09JAN]全流Total Flow[最大流]

    题 裸题不多说,在网络流的练习题里,你甚至可以使用暴力. #include<bits/stdc++.h> using namespace std; typedef long long ll ...

  7. luoguP3128 [USACO15DEC]最大流Max Flow 题解(树上差分)

    链接一下题目:luoguP3128 [USACO15DEC]最大流Max Flow(树上差分板子题) 如果没有学过树上差分,抠这里(其实很简单的,真的):树上差分总结 学了树上差分,这道题就极其显然了 ...

  8. 【luogu P3128 [USACO15DEC]最大流Max Flow】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3128 菜 #include <cstdio> #include <cstring> ...

  9. luogu P3128 [USACO15DEC]最大流Max Flow (树上差分)

    题目描述 Farmer John has installed a new system of N-1N−1 pipes to transport milk between the NN stalls ...

随机推荐

  1. jquery:字符串(string)转json

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...

  2. Ace教你一步一步做Android新闻客户端(三) JSON数据解析

    对于服务器端来说,返回给客户端的数据格式一般分为html.xml和json这三种格式,现在给大家讲解一下json这个知识点, 1 如何通过json-lib和gson这两个json解析库来对解析我们的j ...

  3. PLSQL Developer 客户端工具的安装

    安装之前先把客户端工具instantclient_12_1拷贝到一个没有中文和空格的目录中去, 比如我直接放到了D:\tools下面.注意这里需要的是32bit的. 下面开始安装PLSQL Devel ...

  4. IBM Rational Appscan Part 1

    By Rohit T|July 23rd, 2012 http://resources.infosecinstitute.com/ibm-rational-appscan/ IBM Rational ...

  5. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

  6. 深入理解JavaScript系列(36):设计模式之中介者模式

    介绍 中介者模式(Mediator),用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 主要内容来自:http://www ...

  7. attribute和property的区别

    DOM元素的attribute和property很容易混倄在一起,分不清楚,两者是不同的东西,但是两者又联系紧密.很多新手朋友,也包括以前的我,经常会搞不清楚. attribute翻译成中文术语为“特 ...

  8. Java虚拟机之栈帧

    写在前面的话:Java虚拟机是一门学问,是众多Java大神们的杰作,由于我个人水平有限,精力有限,不能保证所有的东西都是正确的,这里内容都是经过深思熟虑的,部分引用原著的内容,讲的已经很好了,不在累述 ...

  9. centos解决The path "" is not a valid path to the 3.2.0-4-amd64 kernel headers.问题

    我的机子炸了,然后我就得重新装我的虚拟机,再然后我就想去弄好我的共享文件夹安装vmtools,安装的时候出现了一个问题,我忘记以前是怎么解决的,又困扰了我好久 Searching for a vali ...

  10. 如何angular过滤器进行排序???

    首先定义一个json文件: 然后写HTML文件: <div id="box"> <!--第一个下拉框--> <select ng-model=&quo ...