Evacuation Plan-POJ2175最小費用消圈算法

Time Limit: 1000MS Memory Limit: 65536K

Special Judgenode

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

To address this problem, The City Council 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 be not used completely. markdown

The City Council 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 Council, 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 Council’s incompetence.
這裏寫圖片描述ide

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

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). this

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

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

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

The plan in the input file is guaranteed to be valid. Namely, it calls for 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 Council’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 Council’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

Sample Output

SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1
Source

Northeastern Europe 2002

題意:有n個市政大樓和m個避難所,每個市政大樓都有必定的人數,而每個避難所也有必定的容量,從某個市政大樓到某個避難所的花費是曼哈頓距離+1,如今委員會給你一個有效的疏散計劃,判斷還有沒有這個計劃更優的方案。

分析:開始理解題意的時候,覺得用最小費用跑一次,判斷最小費用與所給的答案,可是TLE,後來在討論中看到最小費用會超時,說是用消圈的方式判斷是否是還有更優解。
消圈定理:殘留網絡裏若是存在負費用圈,那麼當前流不是最小費用流。
負圈有必要解釋一下:費用總和是負數,且每條邊的剩餘流量大於0
按照所給的信息建圖。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int INF = 0x3f3f3f3f;

const int Max = 210;

typedef struct node
{
    int x,y,num;
}Point;

int Map[Max][Max],Cost[Max][Max],Num[Max];

Point  Z[Max],B[Max];

int dis[Max],pre[Max],Du[Max];

bool vis[Max];

int n,m,s,t;

int ok(Point a,Point b)
{
    return abs(a.x-b.x)+abs(a.y-b.y)+1;
}

int SPFA() //判斷是否是有負圈
{
    for(int i=0;i<=t;i++)
    {
        dis[i] = INF;

        pre[i] = -1;

        Du[i] = 0;

        vis[i]=false;
    }
    queue<int>Q;

    dis[t] = 0,vis[t] = true;

    Q.push(t);

    Du[t] = 1;

    while(!Q.empty())
    {
        int u = Q.front();

        Q.pop();

        for(int i=0;i<=t;i++)
        {
            if(Map[u][i]&&dis[i]>dis[u]+Cost[u][i])
            {
                dis[i] = dis[u]+Cost[u][i];

                pre[i] = u;

                if(!vis[i])
                {
                    vis[i]=true;

                    Q.push(i);

                    Du[i]++;

                    if(Du[i]>t)
                    {
                        return i;
                    }
                }
            }
        }

        vis[u]=false;
    }

    return -1;
}

int main()
{

    int num;

    while(~scanf("%d %d",&n,&m))
    {
        s= 0, t =n+m+1;

        memset(Map,0,sizeof(Map));

        memset(Cost,0,sizeof(Cost));

        memset(Num,0,sizeof(Num));

        for(int i=1;i<=n;i++) scanf("%d %d %d",&Z[i].x,&Z[i].y,&Z[i].num);

        for(int i=1;i<=m;i++) scanf("%d %d %d",&B[i].x,&B[i].y,&B[i].num);

        for(int i=1;i<=n;i++)//市政與避難所之間建圖
        {
            for(int j=1;j<=m;j++)
            {
                Cost[i][j+n] = ok(Z[i],B[j]);

                Cost[j+n][i] = -Cost[i][j+n];

                Map[i][j+n] = Z[i].num;
            }
        }
        int ans = 0;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&num);

                Map[i][j+n]-= num;

                Map[j+n][i] = num;

                Num[j]+=num;
            }
        }

        for(int i=1;i<=m;i++)
        {
            Map[i+n][t] = B[i].num-Num[i];

            Map[t][i+n] = Num[i];
        }

        ans = SPFA();

        if(ans==-1)
        {
            printf("OPTIMAL\n");
        }
        else
        {
            printf("SUBOPTIMAL\n");

            memset(vis,false,sizeof(vis));

            int v = ans;

            while(!vis[v])
            {
                vis[v]=true;

                v = pre[v];
            }

            ans  = v;

            do
            {
                Map[pre[v]][v] --;

                Map[v][pre[v]]++;

                v = pre[v];
            }
            while(v!=ans);

            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(j!=1)
                    {
                        printf(" ");
                    }

                    printf("%d",Map[j+n][i]);
                }

                printf("\n");
            }
        }
    }
    return 0;
}
相關文章
相關標籤/搜索