ZOJ 1553 Evacuation Plan

最小费用最大流.....

建图:

源点 到 每栋楼  连容量为B,花费为0 的边

每个避难所 到 汇点  连容量为C,花费为0 的边

楼 到 避难所 连容量INF,花费 曼哈顿距离+1 的边

跑费用流后比较.... POJ 2175时限只有一秒.....会超时

Evacuation Plan

Time Limit: 10000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu

[Submit]  
[Go Back]   [Status]

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

Process to the end of file.

Output

If The City Council‘s plan is optimal, then write to the output file 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

3 4

-3 3 5

-2 -2 6

2 2 5

-1 1 3

1 1 4

-2 -2 7

0 -1 3

3 0 1 1

0 0 6 0

0 4 0 1

Sample Output

SUBOPTIMAL

3 0 1 1

0 0 6 0

0 4 0 1

OPTIMAL

Source

Northeastern Europe 2002

[Submit]  
[Go Back]   [Status]

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

using namespace std;

const int maxn=1000;
const int INF=0x3f3f3f3f;

struct Edge
{
    int to,next,cap,flow,cost;
}edge[maxn*maxn];

int Adj[maxn],Size,N;

void init()
{
    Size=0; memset(Adj,-1,sizeof(Adj));
}

void addedge(int u,int v,int cap,int cost)
{
    edge[Size].to=v;
    edge[Size].next=Adj[u];
    edge[Size].cost=cost;
    edge[Size].cap=cap;
    edge[Size].flow=0;
    Adj[u]=Size++;
}

int Add_Edge(int u,int v,int cap,int cost)
{
    addedge(u,v,cap,cost);
    int tk=Size-1;
    addedge(v,u,0,-cost);
    return tk;
}

int dist[maxn],vis[maxn],pre[maxn];

bool spfa(int s,int t)
{
    queue<int> q;
    for(int i=0;i<N;i++)
    {
        dist[i]=INF; vis[i]=false; pre[i]=-1;
    }
    dist[s]=0; vis[s]=true; q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=false;
        for(int i=Adj[u];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].cap>edge[i].flow&&
               dist[v]>dist[u]+edge[i].cost)
            {
                dist[v]=dist[u]+edge[i].cost;
                pre[v]=i;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
    }
    if(pre[t]==-1) return false;
    return true;
}

int MinCostMaxFlow(int s,int t,int &cost)
{
    int flow=0;
    cost=0;
    while(spfa(s,t))
    {
        int Min=INF;
        for(int i=pre[t];~i;i=pre[edge[i^1].to])
        {
            if(Min>edge[i].cap-edge[i].flow)
                Min=edge[i].cap-edge[i].flow;
        }
        for(int i=pre[t];~i;i=pre[edge[i^1].to])
        {
            edge[i].flow+=Min;
            edge[i^1].flow-=Min;
            cost+=edge[i].cost*Min;
        }
        flow+=Min;
    }
    return flow;
}

int n,m;

struct PT
{
    int x,y,c;
}Build[maxn],She[maxn];

int Dist[maxn][maxn],mp[maxn][maxn],liu[maxn][maxn];

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=1;i<=n;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            Build[i]=(PT){a,b,c};
        }
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            She[i]=(PT){a,b,c};
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                Dist[i][j]=abs(Build[i].x-She[j].x)+abs(Build[i].y-She[j].y)+1;
                int t=Add_Edge(i,n+j,INF,Dist[i][j]);
                mp[i][j]=t;
            }
        }
        for(int i=1;i<=n;i++)
        {
            Add_Edge(0,i,Build[i].c,0);
        }
        for(int i=1;i<=m;i++)
        {
            Add_Edge(i+n,n+m+1,She[i].c,0);
        }
        N=n+m+2;
        int flow,cost;
        flow=MinCostMaxFlow(0,n+m+1,cost);

        int C=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&liu[i][j]);
                C+=liu[i][j]*Dist[i][j];
            }
        }

        if(C==cost)
        {
            puts("OPTIMAL");
        }
        else
        {
            puts("SUBOPTIMAL");
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    printf("%d%c",edge[mp[i][j]].flow,(j!=m)?' ':'\n');
                }
            }
        }
    }
    return 0;
}
时间: 2024-10-11 23:15:11

ZOJ 1553 Evacuation Plan的相关文章

POJ 2175 Evacuation Plan 费用流 负圈定理

题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在负环上. 2.这个负环可能包括汇点t,所以构建残量网络的时候也要考虑防空洞到t上的容量. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring

Codeforces Gym 100002 E &quot;Evacuation Plan&quot; 费用流

"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 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 

解题报告 之 POJ2175 Evacuation Plan

解题报告 之 POJ2175 Evacuation Plan 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 term

POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3256   Accepted: 855   Special Judge Description The City has a number of municipal buildings and a number of fallout shelters that were build

POJ 2175 Evacuation Plan

Evacuation Plan Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 217564-bit integer IO format: %lld      Java class name: Main The City has a number of municipal buildings and a number of fallout shelters that

POJ 2175 Evacuation Plan 费用流消圈

题目大意:给出一个费用流的模型和已经流过的一些边,问是否存在比这个解更优的解. 思路:直接用原图做一次费用流求最优解会T掉.先介绍费用流消圈定理:如果当前费用流的残量网络中存在负圈,那么当前流不是最优的解. 其实很好理解,结合原图和流过流量之后的反边,若出现了负圈,那么就可以沿着这个负圈增广,而且费用更小. 不过为了解决这个题我们并不需要建立完整的网络流,只需要建立残量网络之后SPFA看是否能找到负环即可. 具体建立方法: 如果一个避难地点有值,那么T向这个避难地点连边,费用0 若当前避难地点没

POJ - 2175 Evacuation Plan (最小费用流消圈)

题意:有N栋楼,每栋楼有\(val_i\)个人要避难,现在有M个避难所,每个避难所的容量为\(cap_i\),每个人从楼i到避难所j的话费是两者的曼哈顿距离.现在给出解决方案,问这个解决方案是否是花费最小的,若不是,则给出比这个更优的解. 分析:若只是要我们求一个最优解的话就用费用流做.现在要求判断是否最优,那么就是当前这张图中是否最短路还能被更新. 首先需要根据给定的解决方案重现这个状态下的残余网,其实只需要加入必要的弧即可:对与任意的楼与避难所(i,j),建边,费用为其距离;若i->j有流量

POJ2175 Evacuation Plan

嘟嘟嘟 题目大意:给一个费用流的残量网络,判断是不是最优解.如果不是,输出比当前解更优的任意一种方案. <.br> 刚开始以为是水题:建完图后跑费用流,并记录选取方案,最后输出. 然而这样会\(TLE\)! 所以我还是看了题解. 原来用了费用流的一条性质:当前流是最小费用流 \(<=>\)残量网络中没有负圈. 所以做法就是建好残量网络,然后跑\(spfa\)找负环,然后修改环中的边的流量. 具体做法: 1.根据题目可知,从源点向每一栋楼房的边一定都流满了,因此可以不建.而且这也告诉

POJ.2175.Evacuation Plan(消圈)

POJ \(Description\) \(n\)个建筑物,每个建筑物里有\(a_i\)个人:\(m\)个避难所,每个避难所可以容纳\(b_i\)个人. 给出每个建筑物及避难所的坐标,任意两点间的距离为它们的曼哈顿距离\(+1\). 现在给出一个分配方案(\(g[i][j]\)表示第\(i\)个建筑物去第\(j\)个避难所的人数),问是否存在所有人移动的距离之和比当前更小的方案.如果存在,输出任意一组更小的方案. \(n,m\leq100\) \(Solution\) 直接跑费用流会T,但是也没