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. LIBSVM使用方法及参数设置 主要参考了一些博客以及自己使用经验。

    主要参考了一些博客以及自己使用经验.收集来觉得比较有用的. LIBSVM 数据格式需要---------------------- 决策属性  条件属性a  条件属性b  ... 2    1:7   ...

  2. peek函数的用法

    #include <iostream> /* run this program using the console pauser or add your own getch, system ...

  3. WF4.0——升级技能:泛型应用

    前提: 在项目的开发中.我们知道,增加泛型,通过对类型的封装,进行抽象后.能够大大降低我们代码量,在项目中,泛型能够说是高级project师必备的技能之中的一个.也是面向对象的核心"抽象&q ...

  4. VC++使用CSocket发送HTTP Request时需要注意发送数据的编码格式

    VS2010以及更高版本中新建的MFC项目字符集默认是Unicode,CString创建的字符串默认是Unicode. 使用CSocket时,若以CString组织需要发送的HTTP Head时,那么 ...

  5. 扫盲:java中关于路径的问题

    ../FileName:当前工程的上级目录. ./FileName:当前工程所在的目录. /FileName:当前工程所在磁盘的根目录(windows下). FileName:当前工程所在的目录.

  6. perl 中的哈希赋值

    在perl 中,通过代码动态的给哈希赋值,是最常见的应用场景,但是有些情况下,我们事先知道一些信息,当需要把这些信息存放进一个哈希的时候,直接给哈希赋值就好: 哈希的key不用说,就是一个字符串,关键 ...

  7. 利用kseq.h parse fasta/fastq 文件

    在分析中经常需要统计fasta/fastq文件的序列数和碱基数, 但是没有找到一些专门做这件事的小工具,可能是这个功能太简单了: 之前用自己写的perl的脚本统计这些信息, 当fastq文件非常大时, ...

  8. Sqrt算法

    转自原文:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html 一个Sqrt函数引发的血案 2010-10 ...

  9. MyBatis-使用mybatis-generator-core.jar生成POJO和Mapper文件

    Demo: http://pan.baidu.com/s/1pLeyVv9 1.pom.xml <dependencies> <!-- 用于生成日志 --> <depen ...

  10. 学习 TList 类的实现[6]

    实现 TMyList.Add 函数. TList 中的 Add 函数用到了一个 Grow 方法, 它的原理是元素越多就为以后准备更多内存, 我们这里省略为预留 4 个元素的内存; TList 中的 A ...