Poj 2175 evacuation plan)

Source: Internet
Author: User
Tags integer numbers

Http://poj.org/problem? Id = 2175

Evacuation Plan
Time limit:1000 ms   Memory limit:65536 K
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 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 councel 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 not used completely.

The city councel 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 councer, 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 couneller'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 couneller'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 callfor 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 couneller'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 couneller'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

Source

Northeastern Europe 2002.


Question:

There are n buildings and M shelters. The coordinates are given. The number of people in the building and the capacity of the shelters are given. The cost from the building to the shelters is more than 1 in the Manhattan distance. A asylum scheme is provided to determine whether it is optimal (the sum of the spending of all people is the smallest). If not, a better scheme is provided.

Analysis:

It is easy to think of as a billing flow. Connecting the source point to the building edge. The capacity is the number of people in the building, and the unit traffic fee is 0. Connecting the refuge to the settlement point, the capacity is the shelter capacity, and the unit traffic fee is 0; connecting the building to the refuge, the capacity is infinite, and the unit traffic fee is the distance from Manhattan + 1. In this figure, the maximum minimum fee flow is run once. If the minimum fee is lower than the proposed scheme, the corresponding scheme is better (and optimal ).

Although the above idea is correct, it is unfortunate that the efficiency is very low. I have undoubtedly obtained TLE. Is there any better algorithm?

If the best scheme is applied to the stream running at the minimum cost and the maximum flow, the scheme already provided corresponds to a maximum flow, the question requires determining whether the proposed solution is optimal <=> the minimum cost of the stream. If a stream F is a minimum cost stream <=> There is no negative circle in the residual network, and if there is a negative circle, it will be extended along the negative circle to get a stream with lower cost under the same traffic. If spfa is used to judge the negative circle, it is more efficient to judge the negative circle with stacks than the queue in actual tests. It is worth noting that if a point is pushed to the stack/team for n times (assuming N points in total), there is a negative ring in the figure, but this point may not be in the negative loop (isn't it really scary ?), It can only be said that this point has been updated by the negative ring, that is, the negative ring is before this point, so we only need to find the negative ring from this point and then expand in the negative ring, you can get a lower-cost stream.


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm (65536) # define maxn (202) using namespace STD; struct _ point {int X, Y, P;} B [101], SH [101]; int n, m; int FIR [maxn]; int U [maxm], V [maxm], Cap [maxm], flow [maxm], cost [maxm], NEX [maxm]; int e_max; int Q [maxn], d [maxn], Prev [maxn], CNT [maxn]; bool INQ [maxn]; int G [101] [101]; int Shelter [101]; bool vis [maxn]; void add_edge (INT _ u, int _ v, int _ cap, int _ cost, int _ f) {int e; E = e_max ++; U [e] = _ u; V [e] = _ v; CAP [e] = _ cap; cost [e] = _ cost; flow [e] = _ F; NEX [e] = FIR [U [e]; FIR [U [e] = E; E = e_max ++; U [e] = _ v; V [e] = _ u; cap [e] = 0; cost [e] =-_ cost; flow [e] =-_ F; NEX [e] = FIR [U [e]; FIR [U [e] = E;} int negative_loop (int s, int t) {int F, R, Top =-1; F = 0; r =-1; memset (CNT, 0, sizeof CNT); memset (D, 0, sizeof D); memset (INQ, 1, sizeof INQ); // INQ is an array of the bool type, each element occupies 1 byte, so memset (prev,-1, sizeof PREV) can be like this; for (INT I = s; I <= T; I ++) Q [++ R] = I; while (F <= r) {int x = Q [R --]; // stack-based writing, r -- change to f ++ is the queue, and the queue should expand the array. INQ [x] = false; For (int e = FIR [X]; ~ E; E = NEX [e]) {If (Cap [e]> flow [e] & D [V [e]> d [U [e] + cost [e]) {d [V [e] = d [U [e] + cost [E]; Prev [V [e] = E; If (! INQ [V [e]) {q [++ R] = V [E]; INQ [V [e] = true; CNT [V [e] ++; If (CNT [V [e]> T-S + 1) return V [E]; // if you do not want to determine the boundary, increase the value, because if there is no negative loop, it won't be in the queue/stack for so many times. If there is a negative loop, it will go through an infinite loop. }}}return-1 ;} inline int _ cost (const _ point & P1, const _ point & p2) {return ABS (p1.x-p2.x) + ABS (p1.y-p2.y) + 1 ;} int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r", stdin); # endif/online_judge int P; int S, T; scanf ("% d", & N, & M); s = 0; T = n + m + 1; e_max = 0; memset (FIR,-1, sizeof fir); For (INT I = 0; I <n; I ++) {scanf ("% d", & B [I]. x, & B [I]. y, & B [I]. p); add_edge (S, I + 1, B [I]. p, 0, B [I]. p) ;}for (INT I = 0; I <m; I ++) {scanf ("% d", & SH [I]. x, & SH [I]. y, & SH [I]. p);} memset (shelter, 0, sizeof shelter); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {scanf ("% d", & P); Shelter [J] + = P; add_edge (I + 1, N + J + 1, INF ,__ cost (B [I], SH [J]), P) ;}}for (INT I = 0; I <m; I ++) {Add_edge (I + 1 + N, T, SH [I]. p, 0, Shelter [I]);} int K = negative_loop (S, T); If (K! =-1) {puts ("suboptimal"); memset (VIS, 0, sizeof vis); For (int e = Prev [k];! Vis [V [e]; E = Prev [U [e]) // find the negative ring forward {vis [V [e] = true; k = V [E];} For (int e = Prev [k]; E = Prev [U [e]) // augmented in the negative ring {flow [e] ++; // flow [E ^ 1] As long as you find a better solution. --; if (U [e] = k) break;} For (int e = 0; e <e_max; e ++) {If (U [e]> 0 & U [e] <= N & V [e]> N & V [e] <= N + M) G [U [e]-1] [V [e]-n-1] = flow [E];} For (INT I = 0; I <n; I ++) {for (Int J = 0; j <m; j ++) {If (j) putchar (''); printf (" % d ", G [I] [J]);} putchar ('\ n');} else puts ("Optimal"); Return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.