POJ2175:Evacuation Plan(消负圈)
Evacuation Plan
Time Limit: 1000MSMemory Limit: 65536KTotal
Submissions: 5665Accepted: 1481Special Judge
题目链接:http://poj.org/problem?id=2175
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
题意:
给出n个房间,m个目的地,每个房间有一定数量的人,每个目的地也只能装一定数量的人。
现在给出政府大的迁移计划,把n个房间里面的人都迁到m个目的地上面去(通过一个n*m的矩阵给出迁移方案),当然两点建的迁移费用为曼哈顿距离加一。
问现在的迁移计划是否合理,如果不是,请输出一个更合理的方案(special judge)。
题解:
这题可以用最大流最小费用流来做,但是由于边比较多,会T掉。
做这个题需要知道:当前流量下的最小费用<=>当前无负费用圈。
所以我们可以直接让其满流,来判断有无负圈就好了。如果有负圈,就在负圈上面进行相应的退流、加流操作就ok了。
还有需要注意的是,一个点被更新了大于n次,不能说明这个点就在负环中,如下图:

注意一下建图,比较直观的就是直接把最大流时边的状态建出来,类似于下面这样:
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
int tmp;scanf("%d",&tmp);
E[i][j]=tmp;
sum[j]+=tmp;
adde(i,j+n,INF-E[i][j],dis(i,j));
adde(j+n,i,E[i][j],-dis(i,j));
}
}
for(int i=;i<=n;i++) adde(,i,p1[i].w,),adde(i,,,);
for(int i=;i<=m;i++){
adde(i+n,n+m+,p2[i].w-sum[i],);
adde(n+m+,i+n,sum[i],);
}
但是还是可以直接去掉边的容量建图,因为我们跑spfa的时候实际上只需要知道边的费用,但在这种情况下需要知道哪些边应该存在,哪些边不容易存在,不然很容易WA。
另外源点应该连反向边的,但是这里我们其实不需要源点,我们为了跑spfa方便增添了一个正向连边的源点。
其余的边可以根据最后存在的边建出来。
我直接给出第二种建图方式的代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = ;
int n,m,tot;
int E[N][N];
int head[N],vis[N],d[N],pa[N],cnt[N],sum[N];
struct Edge{
int u,v,next,w;
}e[(N*N)<<];
struct Node{
int x,y,w,c;
}p1[N],p2[N];
void adde(int u,int v,int w){
e[tot].w=w;e[tot].v=v;e[tot].u=u;e[tot].next=head[u];head[u]=tot++;
}
int dis(int a,int b){
return abs(p1[a].x-p2[b].x)+abs(p1[a].y-p2[b].y)+;
}
int spfa(){
for(int i=;i<=n+m+;i++) d[i]=1e9;d[]=;
queue<int> q;q.push();memset(vis,,sizeof(vis));vis[]=;
memset(pa,-,sizeof(pa));
memset(cnt,,sizeof(cnt));cnt[]++;
while(!q.empty()){
int u =q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
pa[v]=u;
if(!vis[v]){
vis[v]=;
if(++cnt[v]>n+m+) return v;
q.push(v);
}
}
}
}
return -;
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(head,-,sizeof(head));tot=;
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
scanf("%d%d%d",&p1[i].x,&p1[i].y,&p1[i].w);
for(int i=;i<=m;i++)
scanf("%d%d%d",&p2[i].x,&p2[i].y,&p2[i].w);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
int tmp;scanf("%d",&tmp);
E[i][j]=tmp;
sum[j]+=tmp;
adde(i,j+n,dis(i,j));
if(E[i][j]) adde(j+n,i,-dis(i,j));
}
}
for(int i=;i<=n;i++) adde(,i,);
for(int i=;i<=m;i++){
if(p2[i].w> && sum[i]==) adde(i+n,n+m+,);
else if(sum[i]>&&p2[i].w>sum[i]) adde(n+m+,i+n,),adde(i+n,n+m+,);
else if(p2[i].w==sum[i]) adde(n+m+,i+n,);
}
int s = spfa();
if(s==-) cout<<"OPTIMAL";
else{
cout<<"SUBOPTIMAL"<<endl;
memset(vis,,sizeof(vis));
int tmp=s;
while(true){ //找到在负环中的点,当前求出来的点不一定在负环中
if(!vis[tmp]){
vis[tmp]=;tmp=pa[tmp];
}else{
s=tmp;
break ;
}
}
do{
int u=pa[s];
if(u<=n && s>n) E[u][s-n]++;
if(u>n && s<=n) E[s][u-n]--;
s=u;
}while(s!=tmp);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cout<<E[i][j]<<" ";
}
cout<<endl;
}
}
}
return ;
}
POJ2175:Evacuation Plan(消负圈)的更多相关文章
- POJ 2135.Farm Tour 消负圈法最小费用最大流
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4914 Accepted: 1284 ...
- POJ-2175 Evacuation Plan 最小费用流、负环判定
题意:给定一个最小费用流的模型,根据给定的数据判定是否为最优解,如果不为最优解则给出一个比给定更优的解即可.不需要得出最优解. 解法:由给定的数据能够得出一个残图,且这个图满足了最大流的性质,判定一个 ...
- POJ2175 Evacuation Plan
Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4617 Accepted: 1218 ...
- POJ.2175.Evacuation Plan(消圈)
POJ \(Description\) \(n\)个建筑物,每个建筑物里有\(a_i\)个人:\(m\)个避难所,每个避难所可以容纳\(b_i\)个人. 给出每个建筑物及避难所的坐标,任意两点间的距离 ...
- POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)
http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ 2175:Evacuation Plan(费用流消圈算法)***
http://poj.org/problem?id=2175 题意:有n个楼,m个防空洞,每个楼有一个坐标和一个人数B,每个防空洞有一个坐标和容纳量C,从楼到防空洞需要的时间是其曼哈顿距离+1,现在给 ...
- POJ 2175 Evacuation Plan 费用流 负圈定理
题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在 ...
- poj 2175 Evacuation Plan 最小费用流判定,消圈算法
题目链接 题意:一个城市有n座行政楼和m座避难所,现发生核战,要求将避难所中的人员全部安置到避难所中,每个人转移的费用为两座楼之间的曼哈顿距离+1,题目给了一种方案,问是否为最优方案,即是否全部的人员 ...
- POJ 2175 Evacuation Plan
Evacuation Plan Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origina ...
随机推荐
- Servlet学习笔记06——什么是转发,路径,状态管理?
1.include指令 (1)作用: 告诉容器,在将jsp转换成Servlet时,将 某个文件的内容插入到该指令所在的位置. (2)语法: <%@ include file="&quo ...
- linux命令讲解
1.vi命令 1.光标移动到文件的最后一行 G :$ ]] 2.光标移动到文件的第一行 :0 gg [[ 3.从光标所在位置将光标移动到当前行的开头 0 ^ ...
- composer环境安装
PHP很多优秀的框架,例如Laravel等等,镜像都在国外,相关的包管理工具Composer也是在国外,所以安装失败的可能性很大,题主所示的就是因为连不上Composer.解决方法如下: 进入官网, ...
- LINUX操作系统知识:进程与线程详解
当一个程序开始执行后,在开始执行到执行完毕退出这段时间内,它在内存中的部分就叫称作一个进程. Linux 是一个多任务的操作系统,也就是说,在同一时间内,可以有多个进程同时执行.我们大家常用的单CPU ...
- Pandas基本命令
关键缩写和包导入 在这个速查手册中,我们使用如下缩写: df:任意的Pandas DataFrame对象 同时我们需要做如下的引入: import pandas as pd 创建测试对象 import ...
- B1091 N-自守数 (15分)
B1091 N-自守数 (15分) 如果某个数 \(K\)的平方乘以\(N\) 以后,结果的末尾几位数等于 \(K\),那么就称这个数为"\(N\)-自守数".例如 \(3×92 ...
- 笔记-pytho-语法-yield
笔记-python-语法-yield 1. yield 1.1. yield基本使用 def fab(max): n,a,b = 0, 0, 1 while n < max: y ...
- (D)spring boot使用注解类代替xml配置实例化bean
bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷. 然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎 ...
- Windows Server 2012 新特性:IPAM的配置
Windows Server 2012 中的 IPAM 是一个新增的内置框架,用于发现.监视.审核和管理企业网络上使用的 IP 地址空间.IPAM 可以对运行动态主机配置协议 (DHCP) 和域名服务 ...
- KVO的底层实现原理?如何取消系统默认的KVO并手动触发?
KVO是基于runtime机制实现的 当某个类的属性对象第一次被观察时,系统就会在运行期动态地创建该类的一个派生类(该类的子类),在这个派生类中重写基类中任何被观察属性的setter 方法.派生类在被 ...