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

46 篇文章 0 订阅
Evacuation Plan
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2963 Accepted: 791 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 D i,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 E i,j separated by spaces. E i,j (0 ≤ E i,j ≤ 1000) is a number of workers that shall evacuate from the i th municipal building to the j th 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座建筑物,m个避难所,每座建筑物都有若干个人,每个避难所只能容纳有限的人,每座建筑物与每个避难所都有各自的坐标,点与点之间的距离为曼哈顿距离+1。要使所有建筑物的所有人都能到避难所去,且所有人所走的距离总和最小,题目已给出一个方案,即给出了每座建筑物有各多少个人到每个避难所,现在要你判断这种方案是不是最优的,若不是,则输出一种更优的方案(不一定要最优)。
思路:这题不必求最小费用流,但要用到消圈算法。
定理:一个费用流是最小费用流的充要条件是这个费用流的残量网络没有负费用圈。
         由这个定理,我们只需判断当前方案是否有负费用圈,没有即意味着当前方案为最优方案。
         如果有的话,此时只需沿着负费用圈把各边流量增加1,增流之后残量网络对应的方案肯定是一个更优方案(很容易证明)。
        
        
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64

using namespace std;

const int INF = 1000000000;
const int maxn = 1000;
struct node{
    int x, y;
}a[maxn], b[maxn];
struct Edge{
    int u, v, cost, cap, flow ,next;
}et[maxn * maxn];
int cnt[maxn], pre[maxn], dis[maxn], eh[maxn];
int G[maxn][maxn], cap[maxn], peo[maxn], plan[maxn][maxn], flow[maxn];
bool vis[maxn];
int s, t, n, m, num;
void init(){
    memset(eh, -1, sizeof(eh));
    num = 0;
}
void add(int u, int v, int cost, int cap, int flow){
    Edge e = {u, v, cost, cap, flow, eh[u]};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cost, int cap, int flow){
    add(u, v, cost, cap, flow);
    add(v, u, -cost, 0, -flow);
}
int getdis(node p, node q){
    return abs(p.x - q.x) + abs(p.y - q.y) + 1;
}
void build(){
    init();
    t = n + m + 1;
    for(int i = 1; i <= m; i++) addedge(i + n, t, 0, cap[i], flow[i]);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++)
    addedge(i, j + n, G[i][j], INF, plan[i][j]);
}
bool spfa(int s, int n, int nv){
    queue<int> Q;
    memset(vis, false, sizeof(vis));
    memset(pre, -1, sizeof(pre));
    memset(cnt, 0, sizeof(cnt));
    fill(&dis[0], &dis[maxn], INF);
    dis[s] = 0, vis[s] = true, cnt[s]++;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        vis[u] = false;
        for(int i = eh[u]; i != -1; i = et[i].next)
        {
            int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
            if(cap - flow && dis[v] > dis[u] + cost)
            {
                dis[v] = dis[u] + cost;
                pre[v] = u;
                if(!vis[v])
                {
                    vis[v] = true;
                    Q.push(v);
                    cnt[v]++;
                    if(cnt[v] > n)        //有负环
                    {
                        memset(vis, false, sizeof(vis));
                         u = v;                    //点不一定在负环上
                         while(!vis[u])            //找到负环上任意一个点
                         {
                             vis[u] = true;
                             u = pre[u];
                         }
                         int end = u;        
                         u = pre[v = u];
                         do{                         //更改流量
                             if(u < v && v != t) plan[u][v - nv]++;
                             else if(u > v && u != t) plan[v][u - nv]--;
                             u = pre[v = u];
                         }while(v != end);
                         return false;
                    }
                }
            }
        }
    }
    return true;
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        for(int i = 1; i <= n; i++) scanf("%d%d%d", &a[i].x, &a[i].y, &peo[i]);
        for(int i = 1; i <= m; i++) scanf("%d%d%d", &b[i].x, &b[i].y, &cap[i]);

        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        G[i][j] = getdis(a[i], b[j]);

        for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
        scanf("%d", &plan[i][j]);

        for(int j = 1; j <= m; j++)
        {
            flow[j] = 0;
            for(int i = 1; i <= n; i++)
            flow[j] += plan[i][j];
        }

        build();
        if(spfa(t, t, n)) printf("OPTIMAL\n");
        else
        {
            printf("SUBOPTIMAL\n");
            for(int i = 1; i <= n; i++)
            {
            for(int j = 1; j <= m; j++)
            printf("%d ", plan[i][j]);
            puts("");
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值