Evacuation Plan
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3384   Accepted: 888   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

1 2
-1000 -1000 1000
999 0 600
0 1000 600
550 450

Sample Output

SUBOPTIMAL
3 0 1 1
0 0 6 0
0 4 0 1

SUBOPTIMAL
600 400

题意:一个城市中有n个大楼,m个避难所,接下来给出n个大楼的坐标和人数,接下来m行给出每个避难所的坐标和容纳量,大楼到避难所的距离定义为曼哈顿距离+1,然后给出n行m列数据,第i行第j列代表从第i个大楼到第j个避难所去的人数,保证此方案合法,然后判断此方案是否所有人到避难所的距离和最小,是的话输出OPTIMAL,否则给出更好的方案,但不一定是最优的;
分析:如果用最小费用最大流算法直接求解,会超时,此时有个更巧妙的算法,因为题目中已经给出了一套方案,然后就利用给出的方案建立残留网络图;建图方法如下:
第i个大楼到第j个避难所建立正向边,容量是inf-E[i][j],费用是L[i][j],然后建立反向边,容量是E[i][j],费用是-L[i][j];m个避难所到汇点分别建边,正向边容量是sht[j]-flow[j],费用是0;反向边容量是flow[j],费用是0;因为源点到大楼的流量全部流完,所以可以不用建立源点的图
判断是否有负环存在,从汇点入队开始搜,并且搜索边的容量不是0的边,每次更新的时候顺便更新记录当前点的上一个点保存到pre数组里面;判断完后,入队次数超过图中顶点个数的点不一定在负环当中,所以枚举每个点,利用pre回朔搜索看看是否会搜到被枚举的点,但搜到的话,用数组记录该环中的顶点
然后把i到j的边残余容量E+1,j到i的边残余容量E-1。
程序:

#include"stdio.h"
#include"queue"
#include"string.h"
#include"stdlib.h"
#include"vector"
#define inf 100000000
#define M 333
using namespace std;
int t,head[M],in[M],vis[M],dis[M],flow[M],pre[M],use[M];
struct Lode
{
int x,y,val;
} but[M],sht[M];
int Fab(int a)
{
if(a<0)
a=-a;
return a;
}
struct node
{
int v,val,cost;
node(int V,int VAL,int COST)
{
v=V;
val=VAL;
cost=COST;
}
};
vector<node>edge[M];
int fuck[M],cnt;
int spfa(int s,int n)
{
queue<int>q;
memset(in,0,sizeof(in));
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
for(int i=0; i<=n; i++)
dis[i]=inf;
dis[s]=0;
q.push(s);
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=0; i<(int)edge[u].size(); i++)
{
int v=edge[u][i].v;
if(edge[u][i].val&&dis[v]>dis[u]+edge[u][i].cost)
{
dis[v]=dis[u]+edge[u][i].cost;
pre[v]=u;
if(!vis[v])
{
vis[v]=1;
q.push(v);
in[v]++;
if(in[v]>n)
{
return v;
}
}
}
}
}
return 0;
}
int E[M][M],len[M][M];
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=0; i<=n+m+1; i++)
edge[i].clear();
for(i=1; i<=n; i++)
scanf("%d%d%d",&but[i].x,&but[i].y,&but[i].val);
for(i=1; i<=m; i++)
scanf("%d%d%d",&sht[i].x,&sht[i].y,&sht[i].val);
for(i=1; i<=n; i++)
for(j=1; j<=m; j++)
len[i][j]=Fab(but[i].x-sht[j].x)+Fab(but[i].y-sht[j].y)+1;
memset(flow,0,sizeof(flow));
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
scanf("%d",&E[i][j]);
edge[i].push_back(node(j+n,inf-E[i][j],len[i][j]));
edge[j+n].push_back(node(i,E[i][j],-len[i][j]));
flow[j]+=E[i][j];//到避难所的当前总流量
}
}
for(j=1; j<=m; j++)
{
edge[j+n].push_back(node(n+m+1,sht[j].val-flow[j],0));
edge[n+m+1].push_back(node(j+n,flow[j],0));
}
int ans=spfa(m+n+1,n+m+1);
if(!ans)
{
printf("OPTIMAL\n");
continue;
}
for(i=1; i<=m+n; i++)
{
memset(use,0,sizeof(use));
use[i]=1;
j=pre[i];
int flag=0;
while(j!=-1)
{
use[j]++;
cnt=0;
if(use[j]>=2)
{
fuck[cnt++]=j;
k=pre[j];
while(k!=j&&k!=-1)
{
fuck[cnt++]=k;
k=pre[k];
}
fuck[cnt++]=k;
}
if(cnt)
{
flag++;
break;
}
j=pre[j];
}
if(flag)break;
}
for(i=cnt-1; i>0; i--)
{
if(fuck[i]>=1&&fuck[i]<=n+m&&fuck[i-1]>=1&&fuck[i-1]<=m+n)
{
if(fuck[i]<=n&&fuck[i-1]>n)
E[fuck[i]][fuck[i-1]-n]++;
if(fuck[i]>n&&fuck[i-1]<=n)
E[fuck[i-1]][fuck[i]-n]--;
}
}
printf("SUBOPTIMAL\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
if(j==1)
printf("%d",E[i][j]);
else
printf(" %d",E[i][j]);
}
printf("\n");
}
}
return 0;
}


												

最小费用流判负环消圈算法(poj2175)的更多相关文章

  1. 【dfs判负环】BZOJ1489: [HNOI2009]最小圈

    Description 找出一个平均边权最小的圈. Solution 经典问题,二分答案判断有无负环. 但数据范围大,普通spfa会超时,于是用dfs判负环(快多了). 思路是dis设为0,枚举每个点 ...

  2. [HNOI2009]最小圈 分数规划 spfa判负环

    [HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...

  3. SPFA算法的判负环问题(BFS与DFS实现)

    经过笔者的多次实践(失败),在此温馨提示:用SPFA判负环时一定要特别小心! 首先SPFA有BFS和DFS两种实现方式,两者的判负环方式也是不同的.       BFS是用一个num数组,num[x] ...

  4. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  5. [P1768]天路(分数规划+SPFA判负环)

    题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...

  6. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  7. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  8. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

  9. 【图论】Floyd消圈算法

    毫无卵用的百度百科 Definition&Solution 对于一个给定的链表,如何判定它是否存在环以及环的长度问题,可以使用Floyd消圈算法求出. 从某种意义上来讲,带环的链表在本质上是一 ...

随机推荐

  1. 奇怪的bug:javascript不执行

    背景:有人想要个简单的js效果,点击某个菜单,其他菜单收起. 说了下思路,结果~~ 只好直接写了一个,代码如下: <!DOCTYPE html> <html> <head ...

  2. debian下系列下的apt-get 命令与deb包的手动安装的dpkg命令

    手动下载的deb包的相关操作: 操作deb 使用dpkg 命令工具, dpkg 是Debian package的简写. 下面列举常用的 操作: dpkg –I name.deb  查看 包的详细信息( ...

  3. windows 批处理文件中引用日期

    参见:http://blog.csdn.net/iw1210/article/details/39313677 %DATE%输出的是: yyyy/mm/dd 星期* (例如:2008/12/18 星期 ...

  4. YUV数据YUY2到I420

    /* 主要的采样格式有YCbCr 4:2:0.YCbCr 4:2:2.YCbCr 4:1:1和 YCbCr 4:4:4.其中YCbCr 4:1:1 比较常用,其含义为:每个点保存一个 8bit 的亮度 ...

  5. atitit.javascript调用java in swt attilax 总结

    atitit.javascript调用java in swt attilax 总结 1. BrowserFunction 简单介绍1 1.1. BrowserFunction 能够分为三类: 1 1. ...

  6. C++字符串类型和数字之间的转换

    转载:http://www.cnblogs.com/luxiaoxun/archive/2012/08/03/2621803.html 1.字符串数字之间的转换 字符串---字符数组(1)string ...

  7. ueditor1_4_3_3编辑器的应用

    教程使用的是ueditor1_4_3_3版本. 首先到官网http://ueditor.baidu.com/website/download.html下载jsp  utf-8版 下载好以后,解压,把解 ...

  8. 很好的hadoop学习博客实际操作训练(旧版本)

    实际操作 http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html 流程解析 http://www.cnblogs.com/spo ...

  9. jquery获取html元素的绝对位置和相对位置的方法

    绝对位置坐标: 代码如下: $("#elem").offset().top$("#elem").offset().left 相对父元素的位置坐标: 代码如下: ...

  10. redisTools-IdGenerator

    public class IdGenerator : RedisToolBase { //redis客户端对象 private static readonly NedisClient client = ...