Zoj 1553 evacuation plan

Source: Internet
Author: User
Tags integer numbers


Maximum Flow of minimum cost .....

Graph creation:

The connection capacity from the source point to each building is B, and the cost is 0.

The connection capacity of each refuge to the sink point is C, and the cost is 0.

Building-to-Shelter connection capacity INF, spending more than 1 side of Manhattan


Comparison after the fee stream is run... the poj 2175 time limit is only one second... it will time out.


Evacuation Plan
Time limit:10000 ms   Memory limit:32768kb   64bit Io format:% LLD & % LlU

[Submit] [Go Back] [Status]

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 file 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.

Process to the end of file.


Output

If the city couneller's plan is optimal, then write to the output file 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 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
3 4
-3 3 5
-2-2 6
2 2 5
-1 1 3
1 1 4
-2-2 7
0-1 3
3 0 1 1
0 0 6 0
0 4 0 1


Sample output

Suboptimal
3 0 1 1
0 0 6 0
0 4 0 1
Optimal

Source

Northeastern Europe 2002.

[Submit] [Go Back] [Status]



#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn=1000;const int INF=0x3f3f3f3f;struct Edge{    int to,next,cap,flow,cost;}edge[maxn*maxn];int Adj[maxn],Size,N;void init(){    Size=0; memset(Adj,-1,sizeof(Adj));}void addedge(int u,int v,int cap,int cost){    edge[Size].to=v;    edge[Size].next=Adj[u];    edge[Size].cost=cost;    edge[Size].cap=cap;    edge[Size].flow=0;    Adj[u]=Size++;}int Add_Edge(int u,int v,int cap,int cost){    addedge(u,v,cap,cost);    int tk=Size-1;    addedge(v,u,0,-cost);    return tk;}int dist[maxn],vis[maxn],pre[maxn];bool spfa(int s,int t){    queue<int> q;    for(int i=0;i<N;i++)    {        dist[i]=INF; vis[i]=false; pre[i]=-1;    }    dist[s]=0; vis[s]=true; q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=false;        for(int i=Adj[u];~i;i=edge[i].next)        {            int v=edge[i].to;            if(edge[i].cap>edge[i].flow&&               dist[v]>dist[u]+edge[i].cost)            {                dist[v]=dist[u]+edge[i].cost;                pre[v]=i;                if(!vis[v])                {                    vis[v]=true;                    q.push(v);                }            }        }    }    if(pre[t]==-1) return false;    return true;}int MinCostMaxFlow(int s,int t,int &cost){    int flow=0;    cost=0;    while(spfa(s,t))    {        int Min=INF;        for(int i=pre[t];~i;i=pre[edge[i^1].to])        {            if(Min>edge[i].cap-edge[i].flow)                Min=edge[i].cap-edge[i].flow;        }        for(int i=pre[t];~i;i=pre[edge[i^1].to])        {            edge[i].flow+=Min;            edge[i^1].flow-=Min;            cost+=edge[i].cost*Min;        }        flow+=Min;    }    return flow;}int n,m;struct PT{    int x,y,c;}Build[maxn],She[maxn];int Dist[maxn][maxn],mp[maxn][maxn],liu[maxn][maxn];int main(){    while(scanf("%d%d",&n,&m)!=EOF)    {        init();        for(int i=1;i<=n;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            Build[i]=(PT){a,b,c};        }        for(int i=1;i<=m;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            She[i]=(PT){a,b,c};        }        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                Dist[i][j]=abs(Build[i].x-She[j].x)+abs(Build[i].y-She[j].y)+1;                int t=Add_Edge(i,n+j,INF,Dist[i][j]);                mp[i][j]=t;            }        }        for(int i=1;i<=n;i++)        {            Add_Edge(0,i,Build[i].c,0);        }        for(int i=1;i<=m;i++)        {            Add_Edge(i+n,n+m+1,She[i].c,0);        }        N=n+m+2;        int flow,cost;        flow=MinCostMaxFlow(0,n+m+1,cost);        int C=0;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=m;j++)            {                scanf("%d",&liu[i][j]);                C+=liu[i][j]*Dist[i][j];            }        }        if(C==cost)        {            puts("OPTIMAL");        }        else        {            puts("SUBOPTIMAL");            for(int i=1;i<=n;i++)            {                for(int j=1;j<=m;j++)                {                    printf("%d%c",edge[mp[i][j]].flow,(j!=m)?' ':'\n');                }            }        }    }    return 0;}



Zoj 1553 evacuation plan

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.