Poj2175 algorithm using evacuation plan

Source: Internet
Author: User
Tags integer numbers
This question cannot be directly used with the expense stream. It will be TLE, because only a better solution can be output, so the evacuation plan can be implemented after a round-trip.
Time limit:1000 ms   Memory limit:65536 K
Total submissions:2401   Accepted:616   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
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
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
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<cmath> using namespace std;#define MAXN 5000#define MAXM 1000000#define INF 0xFFFFFFstruct edge{int to,c,w,next;};edge e[MAXM];bool in[MAXN];int head[MAXN],dis[MAXN],pre[MAXN],counts[MAXN],en;int vn,st,ed,n,m;int ax[MAXN],ay[MAXN],az[MAXN],bx[MAXN],by[MAXN],bz[MAXN],bs[MAXN];int cost[MAXN][MAXN];void add(int a,int b,int c,int cc,int d){e[en].to=b;e[en].c=c;e[en].w=d;e[en].next=head[a];head[a]=en++;e[en].to=a;e[en].c=cc;e[en].w=-d;e[en].next=head[b];head[b]=en++;}int distances(int x1,int y1,int x2,int y2){return abs(x1-x2)+abs(y1-y2)+1;}int spfa(int s){queue<int> q;for(int i=0;i<=vn;i++){dis[i]=INF;in[i]=false;pre[i]=-1;counts[i]=0;}dis[s]=0,in[s]=true,q.push(s);counts[s]=1;while(!q.empty()){int tag=q.front();q.pop();in[tag]=false;for(int i=head[tag];i!=-1;i=e[i].next){int j=e[i].to;if(e[i].c && e[i].w+dis[tag]<dis[j]){pre[j]=i,dis[j]=e[i].w+dis[tag];if(!in[j]){q.push(j),in[j]=true;counts[j]++;if(counts[j]>=vn)return j;}}}}return -1;}void update(int p)  {  int flow=INF;int i,u=pre[p];i=u;if(e[u].c<flow) flow=e[u].c;for(i=pre[e[i^1].to];i!=u;i=pre[e[i^1].to]) if(e[i].c<flow) flow=e[i].c;e[u].c-=flow,e[u^1].c+=flow,i=u;for(i=pre[e[i^1].to];i!=u;i=pre[e[i^1].to]) e[i].c-=flow,e[i^1].c+=flow;}  void solve(){st=0,ed=n+m+1,vn=n+m+1;memset(bs,0,sizeof(bs)),en=0;memset(head,-1,sizeof(head));for(int i=1;i<=n;i++)scanf("%d%d%d",&ax[i],&ay[i],&az[i]);for(int i=1;i<=m;i++)scanf("%d%d%d",&bx[i],&by[i],&bz[i]);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cost[i][j]=distances(ax[i],ay[i],bx[j],by[j]);for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){int tempflow;scanf("%d",&tempflow);bs[j]+=tempflow;add(i,j+n,az[i]-tempflow,tempflow,cost[i][j]);}for(int i=1;i<=m;i++)add(i+n,ed,bz[i]-bs[i],bs[i],0);int p;if((p=spfa(ed))==-1){printf("OPTIMAL\n");return ;}memset(in,false,sizeof(in));while(!in[p]){in[p]=true;p=e[pre[p]^1].to;}update(p);printf("SUBOPTIMAL\n");for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){printf("%d",e[(((i-1)*m+j-1)*2)^1].c);if(j!=m)printf(" ");elseprintf("\n");}}int main(){while(scanf("%d%d",&n,&m)!=EOF)solve();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.