(Delete a negative circle) poj 2175 evacuation plan

Source: Internet
Author: User
Tags integer numbers
Evacuation Plan
Time limit:1000 ms   Memory limit:65536 K
Total submissions:968   Accepted:236   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/* Question: Check whether the mayor's solution is optimal. If so, output optimal; otherwise, output suboptimal and better solution. answer: convert it into a model and find out if the current solution is the largest flow with the minimum cost. To verify whether the minimum cost is to find whether the path length is possible or not: Use the bellman method to find whether there is a negative circle that shortens the path without affecting the maximum flow. (that is, to remove a negative circle) PS: This question is very strange. I submitted wa before, but I did not change it later. I submitted it. Source codeproblem: 2175 User: wawadimu memory: 560 K time: 282 Ms language: C ++ result: accepted source code */# include <iostream> # include <algorithm> # include <cmath> # include <queue> using namespace STD; # define INF int_max # define maxn 220 struct node {int I, j; // coordinate int cap;} X [maxn/2], Y [maxn/2]; int dis [maxn] [maxn]; // current maximum traffic from a [T] source point to node t int ans [maxn] [maxn]; // Save the mayor's proposed solution int P [maxn]; // record the path of the previous node int num [maxn], d [maxn]; B OOl vis [maxn]; int n, m; // number of houses and shelters void fun (int s, int t) {int I, j, k; int U, V; memset (VIS, 0, sizeof (VIS); memset (Num, 0, sizeof (Num); queue <int> q; q. push (s); vis [s] = 1; for (I = 0; I <t; I ++) d [I] = inf; d [s] = 0; // d [x] the shortest bool flag from S to X is true; while (! Q. empty () & flag) // find a negative circle {u = Q. front (); q. pop (); vis [u] = false; For (V = 0; v <t; V ++) {If (DIS [u] [v]! = Inf & D [v]> d [u] + dis [u] [v]) // relaxation {d [v] = d [u] + dis [u] [v]; P [v] = u; // record path if (! Vis [v]) // not in the queue {vis [v] = true; q. push (V); num [v] ++ ;}} if (Num [v]> T) {flag = false; break ;}// greater than the number of nodes, proof of negative Ring} If (FLAG) printf ("optimal/N"); else {memset (VIS, 0, sizeof (VIS); While (! Vis [v]) // note that the bounce point is not necessarily the point of the negative circle {// cout <v <"; vis [v] = true; V = P [v];} u = V; while (P [u]! = V) // An error occurred while performing operations on the array as the value (the value may change inexplicably). Therefore, use a temporary variable to store {k = P [u]; // cout <u <Endl; If (U <k) ans [k-M] [u] ++; // person-> room else ans [u-m] [k] --; // room-> person u = K;} If (U <v) ans [V-M] [u] ++; // person-> room else ans [u-m] [v] --; // room-> person printf ("suboptimal/N"); // print Note format problem for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {printf ("% d", ANS [I] [J]); If (J! = M) printf ("") ;}printf ("/N") ;}return ;}int main () {// freopen ("2175.txt"," r ", stdin); int I, j; while (scanf ("% d", & N, & M )! = EOF) {for (I = 1; I <= N; I ++) {scanf ("% d", & X [I]. i, & X [I]. j, & X [I]. CAP); // printf ("% d/N", X [I]. i, X [I]. j, X [I]. CAP) ;}for (I = 1; I <= m; I ++) {scanf ("% d", & Y [I]. i, & Y [I]. j, & Y [I]. CAP); // printf ("% d/N", Y [I]. i, Y [I]. j, Y [I]. CAP);} memset (ANS, 0, sizeof (ANS); for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {scanf ("% d", & Ans [I] [J]); // printf ("% d ", ans [I] [J]); ans [J] [0] + = ans [I] [J];} int S = 0, t = n + m + 1; for (I = 0; I <t; I ++) (J = 0; j <t; j ++) dis [I] [J] = inf; for (I = 1; I <= m; I ++) {If (ANS [I] [0]! = 0) dis [s] [I] = 0; // If (Y [I]. cap-ans [I] [0]) dis [I] [s] = 0; // space available} for (I = 1; I <= N; I ++) {for (j = 1; j <= m; j ++) {dis [I + M] [J] = ABS (X [I]. i-y [J]. i) + ABS (X [I]. j-y [J]. j) + 1; // room-> people are directly away from if (ANS [I] [J]> 0) dis [J] [I + M] =-Dis [I + M] [J]; // person-> negative DISTANCE} fun (S, T );} 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.