解题报告 之 POJ2175 Evacuation Plan

POJ - 2175 Evacuation Plan (网络流, SPFA 消去负环) 题目链接 题意:有 N 栋大楼,M 个避难所,每个大楼里有 Bi 个人,每个避难所有 Ci 的容量,人去避难所避难, 现在给你一个避难的计划,问你这个计划是不是最优的,如果不是最优的,那就找一个比这个优的方案,并输出来。   思路:问的是,这个方案是不是最优的,如果不是,输出一个更优的,并不是输出最优的。 我们要知道,最小费用最大流 等价于 跑完图之后,这个图没有 负环。 所以这个题就... 阅读详情

解题报告 之 POJ2175 Evacuation Plan


Description

The City has a number of municipal buildings and a number of fallout shelters that were build specially to hide municipal workers in case of a nuclear war. Each fallout shelter has a limited capacity in terms of a number of people it can accommodate, and there's almost no excess capacity in The City's fallout shelters. Ideally, all workers from a given municipal building shall run to the nearest fallout shelter. However, this will lead to overcrowding of some fallout shelters, while others will be half-empty at the same time. 

To address this problem, The City Council has developed a special evacuation plan. Instead of assigning every worker to a fallout shelter individually (which will be a huge amount of information to keep), they allocated fallout shelters to municipal buildings, listing the number of workers from every building that shall use a given fallout shelter, and left the task of individual assignments to the buildings' management. The plan takes into account a number of workers in every building - all of them are assigned to fallout shelters, and a limited capacity of each fallout shelter - every fallout shelter is assigned to no more workers then it can accommodate, though some fallout shelters may be not used completely. 

The City Council claims that their evacuation plan is optimal, in the sense that it minimizes the total time to reach fallout shelters for all workers in The City, which is the sum for all workers of the time to go from the worker's municipal building to the fallout shelter assigned to this worker. 

The City Mayor, well known for his constant confrontation with The City Council, does not buy their claim and hires you as an independent consultant to verify the evacuation plan. Your task is to either ensure that the evacuation plan is indeed optimal, or to prove otherwise by presenting another evacuation plan with the smaller total time to reach fallout shelters, thus clearly exposing The City Council's incompetence. 

During initial requirements gathering phase of your project, you have found that The City is represented by a rectangular grid. The location of municipal buildings and fallout shelters is specified by two integer numbers and the time to go between municipal building at the location (Xi, Yi) and the fallout shelter at the location (Pj, Qj) is D  i,j = |Xi - Pj| + |Yi - Qj| + 1 minutes. 

Input

The input consists of The City description and the evacuation plan description. The first line of the input file consists of two numbers N and M separated by a space. N (1 ≤ N ≤ 100) is a number of municipal buildings in The City (all municipal buildings are numbered from 1 to N). M (1 ≤ M ≤ 100) is a number of fallout shelters in The City (all fallout shelters are numbered from 1 to M). 

The following N lines describe municipal buildings. Each line contains there integer numbers Xi, Yi, and Bi separated by spaces, where Xi, Yi (-1000 ≤ Xi, Yi ≤ 1000) are the coordinates of the building, and Bi (1 ≤ Bi ≤ 1000) is the number of workers in this building. 

The description of municipal buildings is followed by M lines that describe fallout shelters. Each line contains three integer numbers Pj, Qj, and Cj separated by spaces, where Pi, Qi (-1000 ≤ Pj, Qj ≤ 1000) are the coordinates of the fallout shelter, and Cj (1 ≤ Cj ≤ 1000) is the capacity of this shelter. 

The description of The City Council's evacuation plan follows on the next N lines. Each line represents an evacuation plan for a single building (in the order they are given in The City description). The evacuation plan of ith municipal building consists of M integer numbers E  i,j separated by spaces. E  i,j (0 ≤ E  i,j ≤ 1000) is a number of workers that shall evacuate from the i  th municipal building to the j  th fallout shelter. 

The plan in the input file is guaranteed to be valid. Namely, it calls for an evacuation of the exact number of workers that are actually working in any given municipal building according to The City description and does not exceed the capacity of any given fallout shelter. 

Output

If The City Council's plan is optimal, then write to the output the single word OPTIMAL. Otherwise, write the word SUBOPTIMAL on the first line, followed by N lines that describe your plan in the same format as in the input file. Your plan need not be optimal itself, but must be valid and better than The City Council's one.

Sample Input

3 4
-3 3 5
-2 -2 6
2 2 5
-1 1 3
1 1 4
-2 -2 7
0 -1 3
3 1 1 0
0 0 6 0
0 3 0 2

Sample Output

SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1


题目大意:有n个办公楼,m个防空洞,现在所有办公楼内的所有人都要跑到某个防空洞去。每个办公楼给出坐标和人数。每个防空洞给出坐标和容纳人数。现在给你一种方案,n*m的矩阵,k=(i , j ) 表示第i个建筑物分配k个人到防空洞 j 。每个人跑到防空洞的时间等于 | x[i] - p[j] | + | y[i] + q[j] | + 1 问你这种方案是否是时间总数最小的?如果不是的话,请给出一个比该方案更好一点儿的方案。

分析:本来想到的是最小费用流,将建筑物和防空洞连接起来,求最小费用流看看总费用和已有方案的费用是否相等,如果不想等则输出最小费用流即可。但是超时,优化了很多次也不行。然后就看了一下其他题解,发现思路是负权回路消除法。首先大家需要明确的是一个流如果是最小费用流,等价于残余网络中没有负载为负的圈。如果发现了那么我们在负圈上增广一下,即可得到一个稍微优一点儿的方案,输出即可。

值得注意的是,建图的时候应该直接构造残余网络,节点为n+m+2个,因为有src和des。

上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<deque>
#include<cmath>
#include<algorithm>
using namespace std;

const int MAXN = 210;
const int MAXM = 43000;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int from, to, cap, next, cost;
};

Edge edge[MAXM];
int head[MAXN];
int prevv[MAXN];
int preve[MAXN];
int has[MAXN];
int senario[MAXN][MAXN];
int dist[MAXN];
int vis[MAXN];
int cntvis[MAXN];
int x[MAXN], y[MAXN], p[MAXN], q[MAXN];
int peo[MAXN], cap[MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap, int cost )
{
	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].cost = cost;
	edge[cnt].next = head[from];
	head[from] = cnt++;

}

int SPFA( int n )
{
	deque<int> dq;
	bool inqueue[MAXN];
	memset( dist, INF, sizeof dist );
	memset( inqueue, 0, sizeof inqueue );
	memset( cntvis, 0, sizeof cntvis );
	memset( prevv, -1, sizeof prevv );
	memset( preve, -1, sizeof preve );
	dq.push_back( src );
	dist[src] = 0;
	inqueue[src] = 1;
	cntvis[src]++;

	while(!dq.empty( ))
	{
		int u = dq.front( );
		dq.pop_front( );
		inqueue[u] = 0;
		for(int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if(edge[i].cap > 0 && dist[u] + edge[i].cost< dist[v])
			{
				dist[v] = dist[u] + edge[i].cost;
				prevv[v] = u;
				preve[v] = i;
				if(!inqueue[v])
				{
					inqueue[v] = 1;
					cntvis[v]++;
					if(cntvis[v] > n) return v;
					if(!dq.empty( ) && dist[v] <= dist[dq.front( )])
						dq.push_front( v );
					else
						dq.push_back( v );
				}
			}
		}
	}
	return -1;
}


void NegaCircle( int ne )
{
	memset( vis, 0, sizeof vis );
	int st = ne;
	while(1)
	{
		if(!vis[st])
		{
			vis[st] = 1;
			st = prevv[st];
		}
		else
		{
			ne = st;
			break;
		}
	}
	do
	{
		int from = prevv[st], to = st; //在负圈上增广
		if(from <= 100 && to > 100)	senario[from][to - 100]++;
		if(from > 100 && to <= 100)senario[to][from - 100]--;
		st = prevv[st];
	} while(st != ne);

}

int main( )
{
	int n, m, f, cost;
	while(cin >> n >> m)
	{
		memset( has, 0, sizeof has );
		memset( head, -1, sizeof head );
		cnt = 0;
		f = 0, cost = 0;
		src = 0, des = 205;
		for(int i = 1; i <= n; i++)
		{
			//cin >> x[i] >> y[i] >> peo[i];
			scanf( "%d%d%d", &x[i], &y[i], &peo[i] );
		}

		for(int i = 1; i <= m; i++)
		{
			//cin >> p[i] >> q[i] >> cap[i];
			scanf( "%d%d%d", &p[i], &q[i], &cap[i] );
		}

		for(int i = 1; i <= n; i++)
		{
			addedge( src, i, peo[i], 0 );
			addedge( i, src, 0, 0 );
		}

		int trans;

		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= m; j++)
			{
				scanf( "%d", &trans );
				senario[i][j] = trans;
				has[j] += trans;
				int c = abs( x[i] - p[j] ) + abs( y[i] - q[j] ) + 1;
				addedge( i, 100 + j, INF - trans, c );
				addedge( 100 + j, i, trans, -c );
			}
		}

		for(int i = 1; i <= m; i++)
		{
			addedge( 100 + i, des, cap[i] - has[i], 0 );
			addedge( des, 100 + i, has[i], 0 );
		}

		int st = SPFA( n + m + 2 );
		if(st == -1)
		{
			printf( "OPTIMAL\n" );
			continue;
		}
		else
		{
			NegaCircle( st );
			printf( "SUBOPTIMAL\n" );
			for(int i = 1; i <= n; i++)
			{
				for(int j = 1; j < m; j++)
				{
					printf( "%d ", senario[i][j] );
				}
				printf( "%d\n", senario[i][m] );
			}
		}

	}
	return 0;
}


最大流接近尾声。勤劳的程序员劳动节还在码代码。。


POJ 2175 Evacuation Plan 最小费用流(找负圈) 点击打开链接 题意:n个居民点(x,y)人数为bi,m个避难点(x,y)容量为ci,两点之间花费为曼哈顿距离, 现在给出n个人到m个避难点的方案,问该方案花费是否最小,若不是则求出最优方案  居民点到避难点容量为min(bi,ci)花费为距离,源点s到居民点容量为bi,花费为0,避难点到汇点T容量为ci,花费为0. num=segma(b[i]),则求流量为num的最小费用流, 阅读详情

相关推荐

消圈法解最小费用流(poj 2175 Evacuation Plan)

消圈算法: 先求最大流, 再在Gf中寻找负费用圈并沿它增广 改进: 增加附加弧(s, t), 费用大于s-t最大费用路(如VC), 弧上的初始流大于s-t最大流(如s出发的弧容量和加1)消圈算法将让尽量多的流移出附加弧, 因此得到的流是最大流. 最后附加弧可能有余量,应当忽略 实际效果: 最小费用增广路算法, 因为Gf中任何s-t路和t-s一定构成负费用圈 最坏情况分析: 每次找负费用圈

新博客地址:sensirly.github.io 3735

有负环的费用流问题:用消消乐“白嫖”的艺术

前文回顾:https://www.cnblogs.com/ofnoname/p/18731222 想象你是一家快递公司的调度员,每天的任务是将货物从仓库高效送到客户。你设计了一条完美路线:每辆卡车都走最短路径,运费最省,按时送达——直到有一天,某个司机突然上报了一个诡异的现象: “老板,我的卡车在某

fenfenfen520a的博客 91

SAP MTO模式实战:从库存管理到成本核算的深度解析

本文深入解析SAP MTO(Make-to-Order)模式在制造业中的应用,从库存管理到成本核算的全流程实战技巧。通过对比MTS模式,详细阐述MTO的核心逻辑、库存管理配置、成本核算路径及与PP模块的集成要点,帮助企业实现按单生产的高效管理,优化库存水平和资金占用。

weixin_29204205的博客 715

网络流小结

第一个问题: 在原图无负环的前提上,为什么增广时的最短路算法不会陷入负环,即为什么增广后的残图不会出现负环? 其实这是一个很浅显的问题,可是我纠结了好长时间,233。 首先假设残图会出现负环,则其出现负环的原因必然是增广后某些反向弧被加入的残图中。 而增广路肯定是无环的,所以这些反向弧只可能是负环的一部分。 设这些反向弧组成的路径为P,P上各反向弧对应的边组成的路径为P',负环的

自在飞花 1114

工业缺陷检测的新范式:2025-2026年零样本检测技术全景扫描

摘要: 工业视觉领域面临缺陷样本稀缺、标注成本高等痛点,零样本缺陷检测(Zero-Shot Defect Detection)成为研究热点。2025-2026年的技术进展主要分为三类:1)视觉-语言模型(VLM)路线,如CLIP适配器跨域对齐热成像数据;2)数字孪生路线,通过CAD模型生成理想状态对比检测缺陷;3)基于CLIP的提示学习,如Crane模型通过上下文提示提升泛化能力。尽管实验室效果显著(如IoU达70%),但在真实工业场景(如MIRAD数据集)中性能平均下降29.78%,凸显跨域泛化仍是挑战。

blanokvaffy的博客 1164

stm32控制舵机_使用URT-1控制飞特舵机上手教程(SM40BL舵机为例)

使用URT-1控制飞特舵机上手教程(SM40BL舵机为例):1,参阅URT-1使用说明.pdf2, 材料清单:① URT-1驱动板② 连接驱动板与电脑的MINI USB 连接线③ 给舵机供电的电源④ 舵机与舵机连接线⑤ 杜邦线(可选)3,将URT-1驱动板与电脑连接4,安装驱动,参阅文件CH340驱动,检查设备管理器串口号。5,舵机连接URT-1驱动板,驱动板接电源,参考下图:6,打开FD软件进行...

weixin_39564510的博客 3437

POJ 2175 Evacuation Plan(网络流消负圈算法)

这是挑战上的一道题。 一开始看到这道题,我是直接套最小费用流上去写的,然后无限T。。。 后来看到,原来不是要找最优的情况,只需要输出一组答案比当前结果花费小即可。 然后看书,才知道有消负圈算法这种东西。 费用流中最优的网络是不存在负圈的,最最简单的理解就是, 可以看到上面这个图,我们费用流是在最大流的前提下找最小费用,但是上面这个图肯定不是最大流,s到t还有1个流量单位。 何为负圈

llzhh的博客 1065

【FPGA】Robei EDA的使用(10)————如何更新EDA的使用license

#海卑而下,能聚众水;学问笃实,以生光辉。 有的时候,有些问题自己通过查找资料解决,宁愿磨难一下,走点弯路,但收获更深刻的认知;不过,有些问题,问一下别人,可能会避免自己耗费过多时间做无用工,这些问题问会的人是极好的。但是怎样把握这个问题的类型,大约只能靠经验了~ (问我为何出此感言,谁叫我纠结了两天的问题,被人家一句话解决,我这个恨啊) ————————————————————正文开始的分割线———————————————— Robei EDA在使用上会有一个注册码,这个license在获取之后,每一次的

Ninquelote的博客 1012

poj 2175

一道很好的题目,看似是费用流,实际上不需要用费用流,用Floyd算法就可以了,当然在Floyd算法中要做一些处理。       开始我一直在想用费用流,可是看了答案后才明白用Floyd算法找出并消去一个负环就可以了。因为题目说“Your plan need not be optimal itself, but must be valid and better than The City Coun

水题收集者 747

Qt Quick/QML入门到精通_专栏demo对应文章目录(目前50个左右demo/长期更新)

自定义图标下拉列表框的效果如下所示:自定义三态按钮的效果如下所示:传送门:53.qt quick-自定义三态按钮、自定义图标下拉列表框_诺谦的博客-CSDN博客传送门:59.qt quick-qml调色板(颜色取色器)实现、自定义QQuickStyle样式_诺谦的博客-CSDN博客63.qt quick-QML侧边滑动栏(不需要任何图片资源,支持自定义左右方向和大小)_诺谦的博客-CSDN博客_qt实现好看侧边栏。...

抵抗时间扭曲,坚持做一件有意义的事 2万+

【时时三省】tessy 进入调试模式 教程

山不在高,有仙则名。水不在深,有龙则灵。----CSDN 时时三省。

时时三省的博客 775

POJ2175 Evacuation Plan

POJ2175 Evacuation Plan Time Limit:1000MS Memory Limit:65536K Total Submissions:1957 Accepted:505 Special Judge Description The City has a number of municipal buil...

weixin_30326515的博客 88

4、高级认证解决方案:Active Directory、LDAP、RADIUS和TACACS+深度解析

本文深入解析了多种高级认证解决方案,包括Active Directory、标准LDAP、RADIUS和TACACS+,涵盖其技术优势、配置步骤及适用场景。同时介绍了如何实现通用用户管理,包括使用第三方认证解决方案和ADSI自助用户管理。通过实际案例部署,展示了如何高效、安全地管理大规模用户认证与权限控制,为企业安全架构提供参考。

o4p5q6r7s的博客 309

POJ 2175 Evacuation Plan 费用流消圈

题目:http://poj.org/problem?id=2175 题意:有n个建筑和m个防空洞,每个建筑里初始有一定数量的人,而防空洞有容量上限,现在让所有在建筑里的人转移到防空洞,从建筑跑到防空洞的花费为他们的曼哈顿距离,现在给出一种转移方案,问是不是最小花费,若不是,给出一种更小的花费(不需要是最小花费,比给定的小即可) 思路:第一次接触到费用流消圈。在本题中,用题目给定的方案构建出残余

弱智就要多努力 870

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

POJ 2175 Evacuation Plan 消圈定理

题意:给出N个建筑物的坐标和里面的人数。给出M个防空洞

leodestiny 1122

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

POJ.2175.Evacuation Plan(消圈)

POJ \(Description\) \(n\)个建筑物,每个建筑物里有\(a_i\)个人;\(m\)个避难所,每个避难所可以容纳\(b_i\)个人。 给出每个建筑物及避难所的坐标,任意两点间的距离为它们的曼哈顿距离\(+1\)。 现在给出一个分配方案(\(g[i][j]\)表示第\(i\)个建筑物去第\(j\)个避难所的人数),问是否存在所有人移动的距离之和比当前更小的方案。如果存在,输出任...

weixin_30352645的博客 202

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

POJ2175:Evacuation Plan(消负圈)

Evacuation Plan Time Limit:1000MSMemory Limit:65536KTotal Submissions:5665Accepted:1481Special Judge 题目链接:http://poj.org/problem?id=2175 Description: The City has a number of municipal b...

dft539533的博客 230

*POJ 2175 Evacuation Plan | 费用流

这题我真是呵呵了,交了13发才A掉了。不过

542

POJ 2175 Evacuation Plan 费用流消圈算法

The City has a number of municipal buildings and a number of fallout shelters that were build specially to hide municipal workers in case of a nuclear war. Each fallout shelter has a limited capacity

sulisulisu的博客 654

Poj 2175 Evacuation Plan (消圈算法)

题目链接:poj 2175 消圈算法,可以用来判断当前的方案是否是令费用最小的方案。若存在一个负环,那么一定可以找到比当前方案更优的方案。对于这道题,我们只需用残量网络建图,跑spfa判断这个图是否有环。若有环,沿着环更改方案即可。具体怎么建图,挖坑待填。 先贴上代码。 #include #include #include #include #include using namespace

顺 858

【费用流消圈】POJ-2175 Evacuation Plan

Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K         Special Judge Description The City has a number of municipal buildings and a number of fallout shelter

JSure的代码库 1047

POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3256   Accepted: 855   Special Judge Description

fcbruce 3344

POJ 2175 Evacuation Plan(费用流消负环)

大意:给出N个建筑物的坐标和里面的人数。给出M个防空洞的坐标和能容纳的人数。一个人的花费定义为从他的建筑物到目标防空洞的曼哈顿距离再加1。现在有人已经设计了一个避难方案。要你判断他给出的方案是否是所有人花费最少的解决方案,如果不是,请给出一个花费更小的解决方案(不必是最优)。 思路:我们可以把,负环中的所有点找到,然后跑费用流,输出残余网络。 #include #include #i

Grit_ICPC的博客 869
上一篇: 解题报告 之 POJ2135 Farm Tour
下一篇: 解题报告 之 POJ3686 The Windy's
StanleyClinton
博客等级 码龄18年 44粉丝 119原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值