最小费用流判负环消圈算法(poj2175)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 3384 | Accepted: 888 | Special Judge |
Description

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
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
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)的更多相关文章
- 【dfs判负环】BZOJ1489: [HNOI2009]最小圈
Description 找出一个平均边权最小的圈. Solution 经典问题,二分答案判断有无负环. 但数据范围大,普通spfa会超时,于是用dfs判负环(快多了). 思路是dis设为0,枚举每个点 ...
- [HNOI2009]最小圈 分数规划 spfa判负环
[HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...
- SPFA算法的判负环问题(BFS与DFS实现)
经过笔者的多次实践(失败),在此温馨提示:用SPFA判负环时一定要特别小心! 首先SPFA有BFS和DFS两种实现方式,两者的判负环方式也是不同的. BFS是用一个num数组,num[x] ...
- 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)
传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...
- [P1768]天路(分数规划+SPFA判负环)
题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于出现在本次试题上了 ...
- [poj3259]Wormholes(spfa判负环)
题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环. 两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...
- poj3259 Wormholes (判负环)【spfa】(模板)
<题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...
- BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划
BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...
- 【图论】Floyd消圈算法
毫无卵用的百度百科 Definition&Solution 对于一个给定的链表,如何判定它是否存在环以及环的长度问题,可以使用Floyd消圈算法求出. 从某种意义上来讲,带环的链表在本质上是一 ...
随机推荐
- 将DataFrame数据如何写入到Hive表中
1.将DataFrame数据如何写入到Hive表中?2.通过那个API实现创建spark临时表?3.如何将DataFrame数据写入hive指定数据表的分区中? 从spark1.2 到spark1.3 ...
- R绘制中国地图,并展示流行病学数据
流行病学的数据讲究“三间分布”,即人群分布.时间分布和空间分布.其中的“空间分布”最好是在地图上展示,才比较清楚.R软件集统计分析与高级绘图于大成,是最适合做这项工作了.关于地图的绘制过程,谢益辉.邱 ...
- 1077. Kuchiguse (20)【字符串处理】——PAT (Advanced Level) Practise
题目信息 1077. Kuchiguse (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The Japanese language is notorious f ...
- MongoDB基础入门视频教程
MongoDB基础入门视频教程http://www.icoolxue.com/album/show/98
- Unity对象查找
1. GameObject.Find 全局摄像机 全局画布 全局灯光 无法查找隐藏对象 ,效率低下,要用完全的路径来提升查找效率 2. transform.Find UI中全部使用此方法 可以查找 ...
- mongoDB的shell数组操作器
http://www.2cto.com/database/201304/205024.html mongoDB数组操作器 $push会向数组末尾加入一个元素,如果数组不存在,则会创建这个数组. 增 ...
- Oracle会话及连接数优化
一.改动Oracle会话及最大连接数 1.查看最大连接数 SQL> show parameter processes; NAME ...
- haffman哈夫曼编码的实现
<span style="font-size:18px;">/* 1.在一棵二叉树中,我们定义从A节点到B节点所经过的分支序列为从A节点到B节点的路径: 定义从A节点到 ...
- rpm源码安装mysql
1)访问官网(mysql社区服务器) http://downloads.mysql.com/archives/community/ 2)选择自己需要的版本和对应服务器(例如 服务器是centos 6. ...
- 【RF库Collections测试】Dictionaries Should Be Equal
Name:Dictionaries Should Be EqualSource:Collections <test library>Arguments:[ dict1 | dict2 | ...