需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为3,最好的方法既是让所有点拆分成两个点,一个点从汇点进入,一个点通向汇点,任意两点的路径则标注为最短路径。

//Dinic算法-拆点构图
//Time:625Ms Memory:2108K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define MAX 405
#define INF 0x3f3f3f3f
#define LL long long
int n, m;
int s, t;
int cow[MAX], hide[MAX];
LL d[MAX][MAX];
int res[MAX][MAX];
int lev[MAX];
void build_map(LL limit) //拆点构图
{
memset(res, 0, sizeof(res));
for (int i = 1; i <= n; i++)
{
res[s][i] = cow[i];
res[i + n][t] = hide[i];
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (d[i][j] <= limit) res[i][j + n] = INF;
}
bool bfs() //构建层次网络
{
memset(lev, -1, sizeof(lev));
queue<int> q;
q.push(s); lev[s] = 0;
while (!q.empty() && lev[t] == -1) {
int cur = q.front(); q.pop();
for (int i = 1; i <= t; i++)
{
if (lev[i] == -1 && res[cur][i])
{
lev[i] = lev[cur] + 1;
q.push(i);
}
}
}
return lev[t] != -1;
}
int dfs(int x, int sum) //增广并更新
{
if (x == t || sum == 0) return sum;
int src = sum;
for (int i = 1; i <= t; i++)
{
if (lev[i] == lev[x] + 1 && res[x][i])
{
int tmp = dfs(i, min(sum, res[x][i]));
res[x][i] -= tmp;
res[i][x] += tmp;
sum -= tmp;
}
}
return src - sum;
}
int Dinic()
{
int maxFlow = 0;
while (bfs())
maxFlow += dfs(0, INF);
return maxFlow;
}
int main()
{
//freopen("in.txt", "r", stdin);
memset(d, INF, sizeof(d));
scanf("%d%d", &n, &m);
s = 0; t = 2 * n + 1;
int cows = 0; //总牛数
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &cow[i], &hide[i]);
cows += cow[i];
}
for (int i = 1; i <= m; i++)
{
int u, v;
LL w;
scanf("%d%d%lld", &u, &v, &w);
d[u][v] = d[v][u] = min(w, d[u][v]);
}
//Floyd
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
{
d[0][i] = d[i][t] = 0;
if (d[i][k] != d[0][0]) {
for (int j = 1; j <= n; j++)
{
if (i != j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
else d[i][j] = 0;
}
}
}
LL l = 0, r = 200LL * 1000000000;
int last;
while (l < r)
{
LL mid = (l + r) / 2;
build_map(mid);
last = Dinic();
last == cows ? r = mid : l = mid + 1;
}
if (r != 200LL * 1000000000)
printf("%lld\n", r);
else printf("-1\n");
return 0;
}

ACM/ICPC 之 网络流-拆点构图(POJ2391)的更多相关文章

  1. ACM/ICPC 之 网络流入门-Ford Fulkerson与SAP算法(POJ1149-POJ1273)

    第一题:按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47M ...

  2. ACM/ICPC 之 网络流入门-EK算法(参考模板)(POJ1273)

    基于残留网络与FF算法的改进-EK算法,核心是将一条边的单向残留容量的减少看做反向残留流量的增加. //网络流 //EK算法 //Time:16Ms Memory:348K #include<i ...

  3. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  4. 【转】ACM/ICPC生涯总结暨退役宣言—alpc55

    转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写 ...

  5. ACM - ICPC World Finals 2013 C Surely You Congest

    原题下载:http://icpc.baylor.edu/download/worldfinals/problems/icpc2013.pdf 题目翻译: 试题来源 ACM/ICPC World Fin ...

  6. 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

  7. 2016 ACM ICPC Asia Region - Tehran

    2016 ACM ICPC Asia Region - Tehran A - Tax 题目描述:算税. solution 模拟. B - Key Maker 题目描述:给出\(n\)个序列,给定一个序 ...

  8. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

  9. hdu 4289 网络流拆点,类似最小割(可做模板)邻接矩阵实现

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  2. LeetCode:Multiply Strings

    题目链接 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  3. Centos6.4 本地yum源配置

    由于单位的服务器均使用的是内网,而安装一些软件如Git,需要很多的依赖包,使用yum安装相对简单,由于不能联网故配置本地yum源配置. 1.首先将需要rpm库添加到系统中: 1).虚拟机中安装的lin ...

  4. C/C++实践笔记 006

    字符与字符串字符按照%d,打印ASCCII字符按%c,打印字符本身‘0’ 0 ‘\0’区别: char ch3=0; 等号会自动转换,转换成ASCCII值所对应的字符.即null或\0C字符串不可以直 ...

  5. MyEclipse建立SpringMVC入门HelloWorld项目

    一.首先,建立空的web project项目: 1. 2. 3. 二.其次,导入先关jar包 1.将jar包导入SpringMVCHelloWorld\WebRoot\WEB-INF\lib目录下 三 ...

  6. angular作用域分析

    angualr作用域 Scope 控制器作用域的继承特性Ⅰ 绑定的数据是变量 单向隔离(兄弟之间互不影响,父子之间单向继承) 父级控制器的数据绑定会影响到子级控制器 前提是子控制器内没有绑定数据 单向 ...

  7. 【BZOJ-3507】通配符匹配 DP + Hash

    3507: [Cqoi2014]通配符匹配 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 372  Solved: 156[Submit][Statu ...

  8. SDL文字和图形

    SDL本身没有显示文字功能,它需要用扩展库SDL_ttf来显示文字.ttf是True Type Font的缩写,ttf是Windows下的缺省字体,它有美观,放大缩小不变形的优点,因此广泛应用很多场合 ...

  9. ARCGIS9.3安装说明

    1) 安装LMSetup.exe"    其中第一步选择第一项,并使用"37102011.efl9"文件做为lic文件,在使用之前需要将该文件中的主机名改为本机的机器名, ...

  10. MySQL数据库命名及设计规范

    1.设计原则 1) 标准化和规范化 数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)通常被认为在性能.扩展性和数据完整性方面达到了最好平衡 ...