Evacuation Plan-POJ2175最小费用消圈算法
| Time Limit: 1000MS | Memory Limit: 65536K |
|---|
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
Source
Northeastern Europe 2002
题意:有n个市政大楼和m个避难所,每一个市政大楼都有一定的人数,而每一个避难所也有一定的容量,从某个市政大楼到某个避难所的花费是曼哈顿距离+1,现在委员会给你一个有效的疏散计划,判断还有没有这个计划更优的方案。
分析:开始理解题意的时候,以为用最小费用跑一次,判断最小费用与所给的答案,但是TLE,后来在讨论中看到最小费用会超时,说是用消圈的方式判断是不是还有更优解。
消圈定理:残留网络里如果存在负费用圈,那么当前流不是最小费用流。
负圈有必要解释一下:费用总和是负数,且每条边的剩余流量大于0
按照所给的信息建图。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int Max = 210;
typedef struct node
{
int x,y,num;
}Point;
int Map[Max][Max],Cost[Max][Max],Num[Max];
Point Z[Max],B[Max];
int dis[Max],pre[Max],Du[Max];
bool vis[Max];
int n,m,s,t;
int ok(Point a,Point b)
{
return abs(a.x-b.x)+abs(a.y-b.y)+1;
}
int SPFA() //判断是不是有负圈
{
for(int i=0;i<=t;i++)
{
dis[i] = INF;
pre[i] = -1;
Du[i] = 0;
vis[i]=false;
}
queue<int>Q;
dis[t] = 0,vis[t] = true;
Q.push(t);
Du[t] = 1;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i=0;i<=t;i++)
{
if(Map[u][i]&&dis[i]>dis[u]+Cost[u][i])
{
dis[i] = dis[u]+Cost[u][i];
pre[i] = u;
if(!vis[i])
{
vis[i]=true;
Q.push(i);
Du[i]++;
if(Du[i]>t)
{
return i;
}
}
}
}
vis[u]=false;
}
return -1;
}
int main()
{
int num;
while(~scanf("%d %d",&n,&m))
{
s= 0, t =n+m+1;
memset(Map,0,sizeof(Map));
memset(Cost,0,sizeof(Cost));
memset(Num,0,sizeof(Num));
for(int i=1;i<=n;i++) scanf("%d %d %d",&Z[i].x,&Z[i].y,&Z[i].num);
for(int i=1;i<=m;i++) scanf("%d %d %d",&B[i].x,&B[i].y,&B[i].num);
for(int i=1;i<=n;i++)//市政与避难所之间建图
{
for(int j=1;j<=m;j++)
{
Cost[i][j+n] = ok(Z[i],B[j]);
Cost[j+n][i] = -Cost[i][j+n];
Map[i][j+n] = Z[i].num;
}
}
int ans = 0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&num);
Map[i][j+n]-= num;
Map[j+n][i] = num;
Num[j]+=num;
}
}
for(int i=1;i<=m;i++)
{
Map[i+n][t] = B[i].num-Num[i];
Map[t][i+n] = Num[i];
}
ans = SPFA();
if(ans==-1)
{
printf("OPTIMAL\n");
}
else
{
printf("SUBOPTIMAL\n");
memset(vis,false,sizeof(vis));
int v = ans;
while(!vis[v])
{
vis[v]=true;
v = pre[v];
}
ans = v;
do
{
Map[pre[v]][v] --;
Map[v][pre[v]]++;
v = pre[v];
}
while(v!=ans);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(j!=1)
{
printf(" ");
}
printf("%d",Map[j+n][i]);
}
printf("\n");
}
}
}
return 0;
}
Evacuation Plan-POJ2175最小费用消圈算法的更多相关文章
- poj 2175 Evacuation Plan 最小费用流判定,消圈算法
题目链接 题意:一个城市有n座行政楼和m座避难所,现发生核战,要求将避难所中的人员全部安置到避难所中,每个人转移的费用为两座楼之间的曼哈顿距离+1,题目给了一种方案,问是否为最优方案,即是否全部的人员 ...
- POJ 2175:Evacuation Plan(费用流消圈算法)***
http://poj.org/problem?id=2175 题意:有n个楼,m个防空洞,每个楼有一个坐标和一个人数B,每个防空洞有一个坐标和容纳量C,从楼到防空洞需要的时间是其曼哈顿距离+1,现在给 ...
- 最小费用流判负环消圈算法(poj2175)
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3384 Accepted: 888 ...
- 【图论】Floyd消圈算法
毫无卵用的百度百科 Definition&Solution 对于一个给定的链表,如何判定它是否存在环以及环的长度问题,可以使用Floyd消圈算法求出. 从某种意义上来讲,带环的链表在本质上是一 ...
- POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]
---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...
- poj2175费用流消圈算法
题意: 有n个建筑,每个建筑有ai个人,有m个避难所,每个避难所的容量是bi,ai到bi的费用是|x1-x2|+|y1-y2|+1,然后给你一个n*m的矩阵,表示当前方案,问当前避难方案是否 ...
- POJ2175 Evacuation Plan
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4617 Accepted: 1218 ...
- nyoj 712 探 寻 宝 藏--最小费用最大流
问题 D: 探 寻 宝 藏 时间限制: 1 Sec 内存限制: 128 MB 题目描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫内处处有 ...
- poj 2195 二分图带权匹配+最小费用最大流
题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...
随机推荐
- iOS进阶篇索引,标记和自定义的table
一.带索引目录的表视图 ①效果图 图1 带索引的列表 ② 数据源 本想获取通讯录中得名字,但为了用模拟器调试方便,就写死了数据,所以也只写了部分字母,总之有那么点意思就成 @interface Vie ...
- java中类继承,到底继承了什么?
继承的最大好处就是为了实现代码的复用.那么,子类到底从父类得到的什么呢? 实例成员 父类的private成员不会被子类继承,子类不能访问.但是子类对象的确包含父类的私有成员. 父类的 包访问成员 继承 ...
- bugs
2016-09-04 10:24:14.503 Scgl[1035:341694] You've implemented -[<UIApplicationDelegate> applica ...
- LINQ教程
在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...
- Python学习【第十一篇】模块(1)
模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 简单地说,模块就是一个保 ...
- linear-gradient----渐变
CSS3 渐变(gradient)可以让你在两个或多个指定的颜色之间显示平稳的过渡. 以前,你必须使用图像来实现这些效果,现在通过使用 CSS3 的渐变(gradients)即可实现.此外,渐变效果的 ...
- CentOS下 pycharm开发环境搭建
经过一系统列的折腾之后,我终于有高版本的python和我熟悉的输入法用了,下面来搭建pycharm下的python开发环境. 1.首先安装java jdk注意是JAVA 的JDK,不是JAVA VM什 ...
- MWeb 1.4 新功能介绍一:引入文件夹到 MWeb 中管理,支持 Octpress、Jekyll 等静态博客拖拽插入图片和实时预览
之前在 MWeb 中打开非文档库中的 Markdown 文档,如果文档中有引用到本机图片,是没办法在 MWeb 中显示出来和预览的.这是因为 Apple 规定在 Mac App Store(MAS) ...
- c#字符显示转换{0:d}
C#:String.Format数字格式化输出 : int a = 12345678; //格式为sring输出// Label1.Text = string.Format("asdfads ...
- UEFI引导在GPT分区下安装win2008——抓住那只傲娇的win2008
上周遇到个客户DELL R520的服务器新采购了8块3T硬盘做备份服务器,raid配置5+1,一个磁21.8T.先用普通的装desktop OS的方法发现进去没raid盘,然后就按照官方的文档进入Li ...