POJ2175 Evacuation Plan

POJ2175 Evacuation Plan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1957 Accepted: 505 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 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. 

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. 

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

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
********************************************************************
题目大意:有n个B点和m个S点,给出了每个点的坐标,每个B点里面有的人数和每个S点可以容纳的人数,让B点里面的人全部转移到S点去,不能超过S点的容纳上界,每个人从一个B点到一个S点所需花费是这两个点的曼哈顿距离+1.现在给出一个分配方案,问这个分配方案下的花费是不是最小的,如果不是就输出一个比这个分配方案优一点的方案即可。
解题思路:本人初学最小费用流,表示做这道题压力巨大,各种看别人解题报告。首先,我没写过最小费用流,知道有个用很多次spfa代替bfs来做的EK来算最小费用流,但是网上的消圈方法着实不会。但为了过这道题,各种看啊。现在终于懂了一点点。原来最小费用流中,自己新加的源点和汇点连出去的边和连进来的边的单位费用都是0,然后,每条边的反向边的单位费用都是这个边的单位费用的相反数。然后,所谓消圈法。消圈法,先求出最大流,然后,我们从汇点出发,汇点!!!从汇点出发进行一次spfa,但每次要走的路必须不是满流。如果找到一个负圈,就说明会有更优解,在这个负圈上,把所有的正向边的流量+1,所有的反向边的流量-1,这样得到的一个流,这个流的流量肯定和原来没有变化,但是费用就下降了这个负圈上的费用正负之和。得到更优解!
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#define N 205
#define M 1000005
#define INF 0x3f3f3f3f
#define abs(a) ((a)>0?(a):-(a))
using namespace std;

int n,m,eid,gk;
int x[N],y[N],pe[N];
int tu[N][N],num[N];
int head[N],nxt[M],ed[M],val[M];
int cnt[N],vis[N],pre[N],dist[N];

void add_edge(int s,int e,int v)
{
    ed[eid]=e;         val[eid]=v;
    nxt[eid]=head[s];  head[s]=eid++;
}

void SPFA(void)
{
    memset(cnt,0,sizeof(cnt));
    memset(vis,0,sizeof(vis));
    queue<int>que;
    for(int i=0;i<=n+m;i++)
        dist[i]=INF;
    vis[gk]=1;
    dist[gk]=0;
    que.push(gk);cnt[gk]++;
    pre[gk]=0;
    int flag=1,e;
    while(!que.empty()&&flag)
    {
        int t=que.front();
        que.pop();
        vis[t]=0;
        for(int i=head[t];~i;i=nxt[i])
        {
            e=ed[i];
            int d=val[i];
            if(dist[e]>dist[t]+d)
            {
                dist[e]=dist[t]+d;
                pre[e]=t;
                if(!vis[e])
                {
                    que.push(e);
                    vis[e]=1;
                    cnt[e]++;
                    if(cnt[e]>=n+m+3)
                    {
                        flag=0;
                        break;
                    }
                }
            }
        }
    }
    if(flag)
        puts("OPTIMAL");
    else
    {
        memset(vis,0,sizeof(vis));
        int s=e;
        while(1)
        {
            if(!vis[s])vis[s]=1,s=pre[s];
            else
                break;
        }
        memset(vis,0,sizeof(vis));
        while(vis[s]==0)
        {
            vis[s]=1;
            int p=pre[s];
            if(p>n&&s!=gk)tu[s][p]--;
            else
                if(p!=gk&&s>n)tu[p][s]++;
            s=pre[s];
        }
        puts("SUBOPTIMAL");
        for(int i=1;i<=n;i++)
        {
            for(int j=1+n;j<=m+n;j++)
            {
                if(j!=1+n)printf(" ");
                printf("%d",tu[i][j]);
            }
            puts("");
        }
    }
}

void re(void)
{
    for(int i=1;i<=n;i++)
        scanf("%d%d%d",&x[i],&y[i],&pe[i]);
    for(int i=1+n;i<=m+n;i++)
        scanf("%d%d%d",&x[i],&y[i],&pe[i]);
    for(int i=1;i<=n;i++)
        for(int j=1+n;j<=m+n;j++)
            scanf("%d",&tu[i][j]);
}

void run(void)
{
    memset(head,-1,sizeof(head));
    memset(num,0,sizeof(num));
    eid=0;gk=0;
    for(int i=1;i<=n;i++)
        for(int j=1+n;j<=m+n;j++)
        {
            int v=abs(x[i]-x[j])+abs(y[i]-y[j])+1;
            add_edge(i,j,v);
            if(tu[i][j])
                add_edge(j,i,-v);
            num[j]+=tu[i][j];
        }
    for(int j=1+n;j<=m+n;j++)
    {
        if(num[j])
            add_edge(gk,j,0);
        if(num[j]<pe[j])
            add_edge(j,gk,0);
    }
    SPFA();
}

int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        re();
        run();
    }

}

  

也许有挫折,但这些,怎能挡住湘北前进的步伐
原文地址:https://www.cnblogs.com/Fatedayt/p/2219260.html