ACM/ICPC 之 网络流-拆点构图(POJ2391)

  需要直接到达,因此源点经过三条边后必须要达到汇点,但为了保证网络流的正确性(路径可反悔),因此不可限制层次网络的最高层次为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;
}
时间: 2024-10-14 12:18:55

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

ACM/ICPC 之 网络流入门-Ford Fulkerson(POJ1149)

按顾客访问猪圈的顺序依次构图(顾客为结点),汇点->第一个顾客->第二个顾客->...->汇点 //第一道网络流 //Ford-Fulkerson //Time:47Ms Memory:276K #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; #def

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 Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

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

转自:http://hi.baidu.com/accplaystation/item/ca4c2ec565fa0b7fced4f811 ACM/ICPC生涯总结暨退役宣言—alpc55 前言 早就该写这篇文章了,但是也很不想去写.毕竟是为之奋斗了两年的目标,不是说舍得就舍得的.然而,自己毕竟是到了该退的时候了,与其扭扭捏捏,不如挥一挥衣袖,尚落得一份潇洒.回首这两年来,有很多是需要总结的.在这里不分巨细的记录下来,或许有点像流水账,但是更多的,是一份对过去的难忘. 童年 我的ACM/ICPC的生

[转]浅谈ACM ICPC的题目风格和近几年题目的发展

斯坦福大学 王颖 ACM ICPC的比赛形式一般是五个小时八个题目,综合考察选手的数学能力.算法能力.coding能力和debug能力,还有团队配合能力.数学方面主要强调组合数学.图论和数论这三个方面的能力:而算法的覆盖范围很广,涉及了大部分经典的算法,和少量较前沿的算法.由于每道题目都需要通过所有的测试数据才能得分,并且需要精确解,这限制了Approximation algorithm在一些NP-hard的题目中的运用,从而使得搜索和剪枝策略对于NP-hard的题目非常重要. Final的题目

HDU 4292 FOOD 2012 ACM/ICPC Asia Regional Chengdu Online

Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3399    Accepted Submission(s): 1141 Problem Description You, a part-time dining service worker in your college’s dining hall, are now confus

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

Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2247    Accepted Submission(s): 940 Problem Description You, the head of Department of Security, recently received a top-secret informatio

《ACM/ICPC 算法训练教程》读书笔记一之数据结构(堆)

书籍简评:<ACM/ICPC 算法训练教程>这本书是余立功主编的,代码来自南京理工大学ACM集训队代码库,所以小编看过之后发现确实很实用,适合集训的时候刷题啊~~,当时是听了集训队final的意见买的,感觉还是不错滴. 相对于其他ACM书籍来说,当然如书名所言,这是一本算法训练书,有着大量的算法实战题目和代码,尽管小编还是发现了些许错误= =,有部分注释的语序习惯也有点不太合我的胃口.实战题目较多是比较水的题,但也正因此才能帮助不少新手入门,个人认为还是一本不错的算法书,当然自学还是需要下不少

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ