POJ 2175 Evacuation Plan

Evacuation Plan

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2175
64-bit integer IO format: %lld      Java class name: Main

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 Di,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 Ei,j separated by spaces. Ei,j (0 ≤ Ei,j ≤ 1000) is a number of workers that shall evacuate from the ith municipal building to the jth 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

Source

Northeastern Europe 2002

解题:消圈法,判费用流是否最优。建立残量网络,从汇点进行spfa,判断后向弧是否形成负权环,是,则有更优解,否则无更有解。如果更优解,只需要,后向弧减1,前向弧加1,可获得一个更有解,虽然可能不是最优解。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 500;
 18 struct arc{
 19     int to,flow,cost,next;
 20     arc(int x = 0,int y = 0,int z = 0,int nxt = -1){
 21         to = x;
 22         flow = y;
 23         cost = z;
 24         next = nxt;
 25     }
 26 };
 27 arc e[maxn*maxn];
 28 int head[maxn],d[maxn],x[maxn],y[maxn],c[maxn];
 29 int tot,S,T,n,m,sf[maxn],bf[maxn],p[maxn],cnt[maxn];
 30 bool in[maxn];
 31 void add(int u,int v,int f1,int f2,int co){
 32     e[tot] = arc(v,f1,co,head[u]);
 33     head[u] = tot++;
 34     e[tot] = arc(u,f2,-co,head[v]);
 35     head[v] = tot++;
 36 }
 37 int getDis(int i,int j){
 38     return abs(x[i] - x[j]) + abs(y[i] - y[j]) + 1;
 39 }
 40 int spfa(){
 41     for(int i = S; i <= T; ++i){
 42         d[i] = INF;
 43         cnt[i] = 0;
 44         in[i] = false;
 45         p[i] = -1;
 46     }
 47     queue<int>q;
 48     d[T] = 0;
 49     q.push(T);
 50     while(!q.empty()){
 51         int u = q.front();
 52         q.pop();
 53         in[u] = false;
 54         for(int i = head[u]; ~i; i = e[i].next){
 55             if(e[i].flow && d[e[i].to] > d[u] + e[i].cost){
 56                 d[e[i].to] = d[u] + e[i].cost;
 57                 p[e[i].to] = i;
 58                 if(!in[e[i].to]){
 59                     in[e[i].to] = true;
 60                     if(++cnt[e[i].to] > T) return e[i].to;
 61                     q.push(e[i].to);
 62                 }
 63             }
 64         }
 65     }
 66     return -1;
 67 }
 68 int main(){
 69     while(~scanf("%d %d",&n,&m)){
 70         memset(head,-1,sizeof(head));
 71         memset(sf,0,sizeof(sf));
 72         memset(bf,0,sizeof(bf));
 73         T = n + m + 1;
 74         S = tot = 0;
 75         for(int i = 1; i <= n + m; ++i)
 76             scanf("%d %d %d",x+i,y+i,c+i);
 77         for(int i = 1; i <= n; ++i){
 78             for(int j = 1; j <= m; ++j){
 79                 int tmp = 0;
 80                 scanf("%d",&tmp);
 81                 add(i,j+n,INF - tmp,tmp,getDis(i,j+n));
 82                 sf[j] += tmp;
 83                 bf[i] += tmp;
 84             }
 85         }
 86         for(int i = 1; i <= n; ++i) add(S,i,c[i] - bf[i],bf[i],0);
 87         for(int i = 1; i <= m; ++i) add(i+n,T,c[i+n] - sf[i],sf[i],0);
 88         int u,v,o = spfa();
 89         if(o == -1) puts("OPTIMAL");
 90         else{
 91             memset(in,false,sizeof(in));
 92             while(!in[o]){
 93                 in[o] = true;
 94                 o = e[p[o]^1].to;
 95             }
 96             v = u = o;
 97             do{
 98                 u = e[p[v]^1].to;
 99                 e[p[v]].flow--;
100                 e[p[v]^1].flow++;
101                 v = u;
102
103             }while(u != o);
104             puts("SUBOPTIMAL");
105             for(int i = 1; i <= n; ++i){
106                 for(int j = 1; j <= m; ++j){
107                     printf("%d%c",e[(i-1)*m*2 + 2*(j-1)+1].flow,j == m?‘\n‘:‘ ‘);
108                 }
109             }
110         }
111     }
112     return 0;
113 }

时间: 2024-08-10 07:52:30

POJ 2175 Evacuation Plan的相关文章

POJ 2175 Evacuation Plan 费用流 负圈定理

题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在负环上. 2.这个负环可能包括汇点t,所以构建残量网络的时候也要考虑防空洞到t上的容量. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring

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 The City has a number of municipal buildings and a number of fallout shelters that were build

POJ.2175.Evacuation Plan(消圈)

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

POJ 2175 Evacuation Plan 费用流消圈

题目大意:给出一个费用流的模型和已经流过的一些边,问是否存在比这个解更优的解. 思路:直接用原图做一次费用流求最优解会T掉.先介绍费用流消圈定理:如果当前费用流的残量网络中存在负圈,那么当前流不是最优的解. 其实很好理解,结合原图和流过流量之后的反边,若出现了负圈,那么就可以沿着这个负圈增广,而且费用更小. 不过为了解决这个题我们并不需要建立完整的网络流,只需要建立残量网络之后SPFA看是否能找到负环即可. 具体建立方法: 如果一个避难地点有值,那么T向这个避难地点连边,费用0 若当前避难地点没

POJ - 2175 Evacuation Plan (最小费用流消圈)

题意:有N栋楼,每栋楼有\(val_i\)个人要避难,现在有M个避难所,每个避难所的容量为\(cap_i\),每个人从楼i到避难所j的话费是两者的曼哈顿距离.现在给出解决方案,问这个解决方案是否是花费最小的,若不是,则给出比这个更优的解. 分析:若只是要我们求一个最优解的话就用费用流做.现在要求判断是否最优,那么就是当前这张图中是否最短路还能被更新. 首先需要根据给定的解决方案重现这个状态下的残余网,其实只需要加入必要的弧即可:对与任意的楼与避难所(i,j),建边,费用为其距离;若i->j有流量

ZOJ 1553 Evacuation Plan

最小费用最大流..... 建图: 源点 到 每栋楼  连容量为B,花费为0 的边 每个避难所 到 汇点  连容量为C,花费为0 的边 楼 到 避难所 连容量INF,花费 曼哈顿距离+1 的边 跑费用流后比较.... POJ 2175时限只有一秒.....会超时 Evacuation Plan Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Stat

Codeforces Gym 100002 E &quot;Evacuation Plan&quot; 费用流

"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 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 

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

POJ 3057 Evacuation 二分图匹配

每个门每个时间只能出一个人,那就把每个门拆成多个,对应每个时间. 不断增加时间,然后增广,直到最大匹配. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<