需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为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. SQL 语句大全(转载)

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

  2. Windows Live Writer 2012 on .net 4

    众所周知,强大的live套件不再更新,止步于2012,所以安装的时候需要.net 3.5的运行时,这很讨厌,在新系统里面是.net 4运行时的,.net运行时的设计者脑袋张虫,新的居然不默认支持旧的程 ...

  3. Linux实现https方式访问站点

    超文本传送协议(HyperText Transfer Protocol,HTML)是一种通信协议,它允许将超文本标记语言文档从web服务器传送到wel浏览器. HTML的特点: 1.支持客户/服务器模 ...

  4. .pop ----remove 删除

    s = {1,2,3,4,5,6,'sn','7'} s.pop()#删除随机值 print(s)#{2, 3, 4, 5, 6, '7', 'sn'} s.remove('sn')#删除值 prin ...

  5. Ubuntu/Mint更换阿里云源

    sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak #备份 sudo vim /etc/apt/sources.list #修改 sudo ...

  6. zend studio(Eclipse)和PyDev搭建Python开发环境

    原文是用Eclipse作开发环境,由于我已经装了zs,而zs也是基于Eclipse的,一试之下发现可以用,呵呵省事了.原文:http://www.cnblogs.com/Realh/archive/2 ...

  7. oracle 修改密码

    [oracle@t-ecif03-v-szzb ~]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.3.0 Production on Mon Nov 1 ...

  8. 【BZOJ-2669】局部极小值 状压DP + 容斥原理

    2669: [cqoi2012]局部极小值 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 561  Solved: 293[Submit][Status ...

  9. MySQL字符串连接函数

    一.CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. select concat(s_id, "--", ...

  10. supervisord安装使用简记

    What is supervisor Supervisor is a client/server system that allows its users to monitor and control ...