POJ 2175 Evacuation Plan 费用流消圈算法

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.

题意:

N个点建筑,M个避难所,要把这N个建筑的人全部转移到避难所里,求比输入中给的方案转移时间更短的方案,不要求新的方案是最优方案(如果没有更优方案输出OPTIMAL,如果有就输出SUBOPTIMAL和新的方案)。

解题思路:

用ford_fulkerson用求出最优解然后和原方案对比

这样做肯定是不行的,会TLE。

消圈吧(存在负圈代表可以在不更改流量的情况下减少费用)。

构造残余网络:

 所有的建筑物i和避难所j,连接ij,边权为正的距离。

         如果原方案ij有人,连接ji,边权为负的距离。

         如果j避难所的人数大于0,连接汇点和j,边权0.

         如果j避难所没有满,连接j和汇点,边权0.

         这样,在残量网络中,容量大于0的边就都建立出来了。

         从汇点出发,找负圈,如果找到了,按照负圈的边,依次更改方案即可。

         比如负圈中有建筑物到i到避难所j的点,ans[i][j]++;

         如果有避难所j到建筑物i的点,ans[i][j]—;

代码:

<pre name="code" class="cpp">#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
using namespace std;
struct edge
{
        int to, next, cost;
}edge[10000000];
int n, m, fir[300], e_max;
inline void add_edge(int from, int to, int cost)
{
    edge[e_max].to = to; edge[e_max].cost = cost;
    edge[e_max].next = fir[from]; fir[from] = e_max++;
}
inline void init()
{
        memset(fir, -1, sizeof(fir));
        e_max = 0;
}
int x[100][3], y[100][3], sum[100], ans[100][100];
int pre[300], vis[300], num[300], dis[300];
int g[100][100];
int q[10000000];
int spfa(int sta, int n)
{
    int r=1,f=0;
    q[0]=sta;
    memset(vis, 0, sizeof(vis));
    memset(num, 0, sizeof(num));
    memset(dis,0x3f3f3f3f,sizeof dis);
    vis[sta] = 1; 
    num[sta]++;
    dis[sta] = 0;
    while(f<r)
    {
        int id =q[f++];
        vis[id] = 0;
        for(int i = fir[id]; i != -1; i = edge[i].next)
            if (dis[id] + edge[i].cost < dis[edge[i].to])
            {
                dis[edge[i].to] = dis[id] + edge[i].cost;
                pre[edge[i].to] = id;
                if (!vis[edge[i].to])
                {
                    vis[edge[i].to] = 1;
                    q[r++]=edge[i].to;
                    if (++num[edge[i].to] > n)
                        return edge[i].to;
                }
            }
    }
    return -1;
}
int main()
{
    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]);
        init();
        int s = n + m;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                g[i][j] = abs(x[i][0] - y[j][0]) + abs(x[i][1] - y[j][1]) + 1;
                add_edge(i, n + j, g[i][j]);
            }
        memset(sum, 0, sizeof(sum));
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
            {
                int tmp;
                scanf("%d", &tmp);
                ans[i][j] = tmp;
                if (tmp != 0)
                        add_edge(j + n, i, -g[i][j]);
                sum[j] += tmp;
            }
        for(int i = 0; i < m; i++)
        {
            if (sum[i] < y[i][2])
                add_edge(i + n, s, 0);
            if (sum[i] > 0)
                add_edge(s, i + n, 0);
        }
        int id = spfa(s, s + 1);
        if (id == -1)
                printf("OPTIMAL\n");
        else
        {
            printf("SUBOPTIMAL\n");
            int sta = id;
            memset(vis, 0, sizeof(vis));
            while(true)
            {
                if (!vis[sta])
                {
                        vis[sta] = true;
                        sta = pre[sta];
                }
                else
                {
                        id = sta;
                        break;
                }
            }
            do
            {
                int from = pre[sta], to = sta;
                if (from < n && to >= n && to < s)
                        ans[from][to - n]++;
                if (to < n && from >= n && from < s)
                        ans[to][from - n]--;
                sta = pre[sta];
            }while(sta != 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]);
                printf("\n");
            }
        }
    }
         return 0;
}


 
  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值