POJ2175 evacuation Plan for the report on problem solving

Source: Internet
Author: User
Tags integer numbers

POJ2175 Evacuation Plan for the report on problem solving

Description

The city have 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 have a limited capacity in terms of a number of people it can accommodate, and there ' s almost no exces s capacity in the city ' s fallout shelters. Ideally, all workers from a given municipal building shall run to the nearest fallout shelter. However, this'll leads to overcrowding of some fallout shelters, while others'll be half-empty at the same time.

To address this problem, the city council have developed a special evacuation plan. Instead of assigning every worker to a fallout shelter individually (which would be a huge amount of information to keep), They allocated fallout shelters to municipal buildings, listing the number of workers from every building this 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 is assigned to fallout shelters, and a Li mited capacity of each fallout Shelter-every Fallout shelter are assigned to no more workers then it can accommodate, tho Ugh some fallout shelters may is not used completely.

The city council claims that their evacuation plan was optimal, in the sense of it minimizes the total time to reach fall Out shelters for all workers in the city, which are the sum for all workers of the time to go from the worker ' s municipal b Uilding to the fallout shelter assigned to this worker.

The city Mayor, well known for he constant confrontation with the city Council, does not buy their claim and hires as An independent consultant to verify the evacuation plan. Your task is to either ensure, the evacuation plan was indeed optimal, or to prove otherwise by presenting another Evac Uation 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 had found that the city was represented by a rectangular Grid. The location of municipal buildings and fallout shelters are specified by both integer numbers and the time to go between MU Nicipal Building at the location (Xi, Yi) and the fallout shelter on the location (Pj, Qj) are 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 numbers N and M separated by a space. N (1≤n≤100) is a number of municipal buildings in the city (all municipal buildings was numbered from 1 to N). M (1≤m≤100) is a number of fallout shelters in the city (all fallout shelters was 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) is 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) is 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 is 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, shall evacuate from the i-th municipal building to the J th Fallout S Helter.

The plan in the input file was guaranteed to be valid. Namely, it calls for a evacuation of the exact number of workers that is actually working in any given municipal Buildin G According to the city description and does no exceed the capacity of any given fallout shelter.

Output

If The City Council's plan is optimal and then write to the output of 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 being optimal itself, but must is 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


The main idea: there are n office buildings, M-bomb shelters, and now all the buildings in the office of everyone to run to a bomb shelter. The coordinates and the number of people are given for each office building. Each bomb shelter gives the coordinates and number of seats. Now give you a solution to the n*m matrix, k= (i, J) represents the first building allocation K person to the bomb shelter J. The time everyone runs to the Bomb shelter equals | X[i]-P[J] | + | Y[i] + q[j] | + 1 Ask you if this plan is the smallest amount of time? If not, please give a better solution than the solution.
Analysis: Originally thought is the minimum cost flow, the building and the bomb shelter connected together, to find the minimum cost flow to see if the total cost and the cost of the existing scheme is equal, if you do not want to wait for the output of the minimum cost flow. But time-outs have been optimized many times. Then looked at the other solution, found that the idea is negative power loop elimination method. First of all, one needs to be clear that a stream, if it is a minimum cost stream, is equivalent to a loop with no negative load in the remaining network. If we find it, then we can widen it in the negative circle and get a slightly better solution to the output.
It should be noted that the construction of the time should be directly constructed residual network, the node is n+m+2, because there are src and Des.
on the code:
#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;st Ruct Edge{int from, to, caps, 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 Adde Dge (int from, int to, int caps, int cost) {Edge[cnt].from = From;edge[cnt].to = To;edge[cnt].cap = Cap;edge[cnt].cost = Co St;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 Inque UE); memset (Cntvis, 0, sizeof Cntvis), memset (PREVV,-1, sizeof PREVV), memset (Preve,-1, sizeof Preve);d Q.push_back ( SRC);d ist[src] = 0;inqueue[src] = 1;cntvis[src]++;while (!dq.empty ()) {int U = Dq.front ();d q.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;i F (!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;//augmented If on negative circle (from <= && to >) senario[from][to-100]++;if (From > && to <=) senario[to][from-100]--;st = prevv[st];} while (st! = NE);} int main () {int n, m, F, Cost;while (Cin >> n >> m) {memset (have, 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], &A Mp;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;ha S[J] + = Trans;int c = ABS (X[i]-p[j]) + ABS (Y[i]-q[j]) + 1;addedge (i, + J, Inf-trans, c); Addedge (+ j), I, trans,-C);}} for (int i = 1; I <= m; i++) {Addedge (+ I, DES, cap[i]-has[i], 0), Addedge (DES, + I, Has[i], 0);} int st = SPFA (n + M + 2), if (st = = 1) {printf ("optimal\n"); continue;}  Else{negacircle (ST);p rintf ("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;}


The maximum flow is nearing the end. Hard-working programmers Labor Day still in code code.


POJ2175 evacuation Plan for the report on problem solving

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.