解题报告 之 POJ2175 Evacuation Plan

解题报告 之 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;
}

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

时间: 2024-07-28 22:15:40

解题报告 之 POJ2175 Evacuation Plan的相关文章

解题报告 之 POJ3057 Evacuation

解题报告 之 POJ3057 Evacuation Description Fires can be disastrous, especially when a fire breaks out in a room that is completely filled with people. Rooms usually have a couple of exits and emergency exits, but with everyone rushing out at the same time

POJ2175 Evacuation Plan

嘟嘟嘟 题目大意:给一个费用流的残量网络,判断是不是最优解.如果不是,输出比当前解更优的任意一种方案. <.br> 刚开始以为是水题:建完图后跑费用流,并记录选取方案,最后输出. 然而这样会\(TLE\)! 所以我还是看了题解. 原来用了费用流的一条性质:当前流是最小费用流 \(<=>\)残量网络中没有负圈. 所以做法就是建好残量网络,然后跑\(spfa\)找负环,然后修改环中的边的流量. 具体做法: 1.根据题目可知,从源点向每一栋楼房的边一定都流满了,因此可以不建.而且这也告诉

解题报告 之 POJ2391 Ombrophobic Bovines

解题报告 之 POJ2391 Ombrophobic Bovines Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the rain makes them shake in their hooves. They have decided to put a rain siren on the farm to let them know when rai

解题报告 之 UVA563 Crimewave

解题报告 之 UVA563 Crimewave Description Nieuw Knollendam is a very modern town. This becomes clear already when looking at the layout of its map, which is just a rectangular grid of streets and avenues. Being an important trade centre, Nieuw Knollendam a

解题报告 之 HDU5301 Buildings

解题报告 之 HDU5301 Buildings Description Your current task is to make a ground plan for a residential building located in HZXJHS. So you must determine a way to split the floor building with walls to make apartments in the shape of a rectangle. Each buil

解题报告 之 SOJ1942 Foto

解题报告 之 SOJ1942 Foto Description The election campaign just started, so PSOS decided to make some propagation. One of the representatives came with a great idea - he proposes to make an photography of their Parliament Club. Unfortunatelly, even after

POJ 2175 Evacuation Plan

Evacuation Plan Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 217564-bit integer IO format: %lld      Java class name: Main The City has a number of municipal buildings and a number of fallout shelters that

hdu 1541 Stars 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 题目意思:有 N 颗星星,每颗星星都有各自的等级.给出每颗星星的坐标(x, y),它的等级由所有比它低层(或者同层)的或者在它左手边的星星数决定.计算出每个等级(0 ~ n-1)的星星各有多少颗. 我只能说,题目换了一下就不会变通了,泪~~~~ 星星的分布是不是很像树状数组呢~~~没错,就是树状数组题来滴! 按照题目输入,当前星星与后面的星星没有关系.所以只要把 x 之前的横坐标加起来就可以了

【百度之星2014~初赛(第二轮)解题报告】Chess

声明 笔者最近意外的发现 笔者的个人网站http://tiankonguse.com/ 的很多文章被其它网站转载,但是转载时未声明文章来源或参考自 http://tiankonguse.com/ 网站,因此,笔者添加此条声明. 郑重声明:这篇记录<[百度之星2014~初赛(第二轮)解题报告]Chess>转载自 http://tiankonguse.com/ 的这条记录:http://tiankonguse.com/record/record.php?id=667 前言 最近要毕业了,有半年没做