解题报告 之 POJ2175 Evacuation Plan
来源:互联网 发布:炒外汇交易软件 编辑:程序博客网 时间:2024/05/02 00:11
解题报告 之 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.
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.
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 62 2 5-1 1 31 1 4-2 -2 70 -1 33 1 1 00 0 6 00 3 0 2
Sample Output
SUBOPTIMAL3 0 1 10 0 6 00 4 0 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 );elsedq.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;}
最大流接近尾声。勤劳的程序员劳动节还在码代码。。
0 0
- 解题报告 之 POJ2175 Evacuation Plan
- poj2175 Evacuation Plan (mcmf消圈算法)
- 解题报告 之 POJ3057 Evacuation
- Evacuation Plan-POJ2175最小费用消圈算法
- poj2175 Evacuation Plan 消圈算法(小组赛G)
- POJ2175 Evacuation Plan 【 最小费用流+消圈定理】
- Evacuation Plan
- hdu 3757 Evacuation Plan
- UVALive 4987 Evacuation Plan
- UVA 1474 Evacuation Plan
- HDU 3757 Evacuation Plan
- UVALive - 4987 Evacuation Plan
- POJ-2175-Evacuation Plan
- ZOJ 1553 Evacuation Plan
- UVA 1474 Evacuation Plan
- POJ 2175 Evacuation Plan
- HDU-3757-Evacuation Plan
- HDU 4671 Backup Plan 解题报告
- 【MFC】基于opencv的趣味相机
- Testing Method - 读书笔记 ( 二 )
- CUDA编程
- 【hdoj 1002】大数加法
- Discuz x2.5 论坛性能优化速度优化的方法 - 让你论坛秒开
- 解题报告 之 POJ2175 Evacuation Plan
- 将char 转换成 int
- [授权发表]为什么计算机的学生要学习 Linux 开源技术
- 几种输入的方法
- 调整数组顺序使奇数位于偶数前面
- Java Web入门之网络聊天室
- eclipse 4.32 安装 gradle
- OpenGL ES2.0编程三步曲
- English Contest -- The Programmer's Dream