poj 2175 Evacuation Plan(最小费用流 (消圈算法))

Evacuation Plan
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 3689
Accepted: 980
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 jthfallout 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

Source


题目描述:
有n个建筑物,m个避难所,告诉你每个建筑物的坐标和拥有的人。告诉你每个避难所的坐标和容量。建筑物i和避难所j的距离为|xi-xj| + |yi-yj| + 1。现在要求把建筑物的人都疏散到避难所。给你一个方案,n行m列,i行j列表示建筑物i有x[i][j]的人去j避难所。
问你这个方案是不是最优的,不是的话,输出一个比给定方案好的方案即可(SPJ)。
解题报告:
如果直接求最小费用流求出最优值输出,算法正确,但是会TLE,注意到题目不一定要求输出最优,好一点就可以。
“可行流x为最小费用流的充要条件是残量网络中不存在负费用增广圈”
按照这个条件,我们建立残量网络即可,由于要判断负圈,所以只要剩余容量大于0,就连接边即可,走几遍都无所谓。
为了简化,可以不需要原来的源点(因为需要源点满流,所以不会沿着建筑物点走回源点当然建立了也无所谓)。
省下的就是残量网络建图:
所有的建筑物i和避难所j,连接ij,边权为正的距离。
如果原方案i到j有人,连接ji,边权为负的距离。
如果j避难所的人数大于0,连接汇点和j,边权0.
如果j避难所没有满,连接j和汇点,边权0.
这样,在残量网络中,容量大于0的边就都建立出来了。
从汇点出发,找负圈,如果找到了,按照负圈的边,依次更改方案即可。
比如负圈中有建筑物到i到避难所j的点,x[i][j]++
如果有避难所j到建筑物i的点,x[i][j]—
输出方案即可。

参考自:点我点我点我

<span style="font-family:KaiTi_GB2312;font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 100000000
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

struct Edge
{
    int to,next,cost;
}edge[100000];
int n,m;
int head[300],tol;
void addedge(int u,int v,int cost)
{
    edge[tol].to=v;
    edge[tol].cost=cost;
    edge[tol].next=head[u];
    head[u]=tol++;
}
int x[110][3],y[110][3],sum[110],ans[110][110];
int pre[300],vis[300],num[300],dis[300];
//pre记录负环,num入队列的次数,spfa返回最先入队>n的点
int len[110][110];
int spfa(int s,int n)
{
    queue<int> q;
    MEM(vis,0);
    MEM(num,0);
    for(int i=0;i<n;i++)
        dis[i]=INF;
    q.push(s);
    vis[s]=1;
    num[s]++;
    dis[s]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dis[u]+edge[i].cost<dis[v])
            {
                dis[v]=dis[u]+edge[i].cost;
                pre[v]=u;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                    if(++num[v]>n)  return v;
                }
            }
        }
    }
    return -1;
}

int main()
{
//    fread;
//    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d%d%d",&x[i][0],&x[i][1],&x[i][2]);
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&y[i][0],&y[i][1],&y[i][2]);
        MEM(head,-1);
        tol=0;
        int s=n+m;//汇点
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                len[i][j]=abs(x[i][0]-y[j][0])+abs(x[i][1]-y[j][1])+1;
                addedge(i,j+n,len[i][j]);
            }
        }
        MEM(sum,0);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&ans[i][j]);
                if(ans[i][j]!=0) addedge(j+n,i,-len[i][j]);
                sum[j]+=ans[i][j];
            }
        }
        for(int i=0;i<m;i++)
        {
            if(sum[i]<y[i][2])  addedge(i+n,s,0);
            if(sum[i]>0)  addedge(s,i+n,0);
        }
        int id=spfa(s,s+1);
        if(id==-1) puts("OPTIMAL");
        else
        {
            puts("SUBOPTIMAL");
            int st=id;
            MEM(vis,0);
            while(1)
            {
                if(!vis[st])
                {
                    vis[st]=1;
                    st=pre[st];
                }
                else
                {
                    id=st;
                    break;
                }
            }
            do
            {
                int u=pre[st],v=st;
                if(u<n&&v>=n&&v<s)  ans[u][v-n]++;
                if(v<n&&u>=n&&u<s)  ans[v][u-n]--;
                st=pre[st];
            }while(st!=id);
            for(int i=0;i<n;i++)
            {
                printf("%d",ans[i][0]);
                for(int j=1;j<m;j++)
                    printf(" %d",ans[i][j]);
                puts("");
            }
        }

    }
    return 0;
}
</span>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值