POJ 2135.Farm Tour 消负圈法最小费用最大流
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 4914 | Accepted: 1284 | Special Judge |
Description

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 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 jthfallout 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
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 题目链接:http://poj.org/problem?id=2175题意:有n栋建筑,m个防空洞,现在市政府给出了一个逃生方案,问有没有一个更好的方案,有的话,输出方案。
思路:最小费用最大流。流量为人数,花费为距离。不知道是不是写矬了,用连续最短路求最小费用最大流一直TLE。改成用消负圈法,即不断的消去负圈而得到最小费用流。应为是任意两点间的最短路径并且含有负边,用Floyd算法。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define PI acos(-1.0)
const int maxn=3e2+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
const ll INF=1e18+;
int n,m;
int NN;
struct node
{
int x,y;
int cou;
} b[maxn],c[maxn];
int E[maxn][maxn];
int g[maxn][maxn];
int prevv[maxn][maxn];
int used[maxn];
void solve()
{
for(int i=; i<NN; i++)
{
for(int j=; j<NN; j++)
prevv[i][j]=i;
}
for(int k=; k<NN; k++)
{
for(int i=; i<NN; i++)
{
for(int j=; j<NN; j++)
{
if(g[i][j]>g[i][k]+g[k][j])
{
g[i][j]=g[i][k]+g[k][j];
prevv[i][j]=prevv[k][j];
if(i==j&&g[i][j]<)
{
fill(used,used+NN,);
for(int v=i; !used[v]; v=prevv[i][v])
{
used[v]=;
if(v!=n+m+&&prevv[i][v]!=n+m+)
{
if(v>n) E[prevv[i][v]][v-n]++;
else E[v][prevv[i][v]-n]--;
}
}
cout<<"SUBOPTIMAL"<<endl;
for(int x=; x<=n; x++)
{
for(int y=; y<m; y++)
cout<<E[x][y]<<" ";
cout<<E[x][m]<<endl;
}
return;
}
}
}
}
}
cout<<"OPTIMAL"<<endl;
}
int main()
{
scanf("%d%d",&n,&m);
int s=,t=n+m+;
NN=t+;
int f=;
for(int i=; i<=n; i++)
scanf("%d%d%d",&b[i].x,&b[i].y,&b[i].cou);
for(int i=; i<=m; i++)
scanf("%d%d%d",&c[i].x,&c[i].y,&c[i].cou);
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
scanf("%d",&E[i][j]);
}
for(int i=; i<NN; i++)
fill(g[i],g[i]+NN,inf);
for(int j=; j<=m; j++)
{
int sum=;
for(int i=; i<=n; i++)
{
int d=abs(b[i].x-c[j].x)+abs(b[i].y-c[j].y)+;
g[i][j+n]=d;
if(E[i][j]>) g[j+n][i]=-d;
sum+=E[i][j];
}
if(sum>) g[n+m+][j+n]=;
if(sum<c[j].cou) g[j+n][n+m+]=;
}
solve();
return ;
}
消负圈法求最小费用最大流
POJ 2135.Farm Tour 消负圈法最小费用最大流的更多相关文章
- POJ 2135 Farm Tour (网络流,最小费用最大流)
POJ 2135 Farm Tour (网络流,最小费用最大流) Description When FJ's friends visit him on the farm, he likes to sh ...
- poj 2135 Farm Tour 【无向图最小费用最大流】
题目:id=2135" target="_blank">poj 2135 Farm Tour 题意:给出一个无向图,问从 1 点到 n 点然后又回到一点总共的最短路 ...
- hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流
/** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- POJ 2135 Farm Tour(最小费用最大流)
Description When FJ's friends visit him on the farm, he likes to show them around. His farm comprise ...
- POJ 3422 Kaka's Matrix Travels (最小费用最大流)
POJ 3422 Kaka's Matrix Travels 链接:http://poj.org/problem? id=3422 题意:有一个N*N的方格,每一个方格里面有一个数字.如今卡卡要从左上 ...
- POJ 2135 Farm Tour (费用流)
[题目链接] http://poj.org/problem?id=2135 [题目大意] 有一张无向图,求从1到n然后又回来的最短路 同一条路只能走一次 [题解] 题目等价于求从1到n的两条路,使得两 ...
- POJ 2135 Farm Tour 最小费用流
两条路不能有重边,既每条边的容量是1.求流量为2的最小费用即可. //#pragma comment(linker, "/STACK:1024000000,1024000000") ...
随机推荐
- RecycleView 使用自定义CardLayouManager内容无法滚动问题
1.开始一直反应不过来一个问题:RecycleView不是自带滚动效果吗?为啥子条目还不能全部滚动,显示出来呢? 意识到:只有当RecycleView 适配器中条目数量特别多,才可以滚动. 然而自己的 ...
- spring-boot-devtools Idea 热部署
1 pom.xml文件 注:热部署功能spring-boot-1.3开始有的 <!--添加依赖--> <dependency> <groupId>org.sprin ...
- 第五篇:jmeter图形监控扩展
插件下载:http://jmeter-plugins.org/downloads/all/ 插件: 1.首先将jmeterPluging.jar包复制到jmeter的lib目录下面的ext目录下面,然 ...
- 开启swap交换分区
开启swap 1.创建用于交换分区的文件: dd if=/dev/zero of=/mnt/swap bs=1M count=2048 注:block_size.number_of_block 大小可 ...
- 手工命令行 搭建 hadoop 和 spark 环境
环境准备:3台CentOS7,64位,Hadoop2.7需要64位Linux 192.168.20.161 192.168.20.162 192.168.20.163 三台机器分别叫host01. ...
- 数据库类型空间效率探索(三)-char
测试环境 表信息 表数据量22.23万,占用空间44.494M 用到的sql语句 增加列:alter table t_type add column new_column char(1) defaul ...
- GIT 命令集
Git图形化界面 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remot ...
- navicat连接虚拟机中mysql"Access denied for user'root'@'IP地址'"问题
登陆要链接的服务器上的mysql 命令:mysql -uroot -p123456 然后执行 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED ...
- PL/SQL Dev连接Oracle弹出空白提示框的解决方法分享
第一次安装Oracle,装在虚拟机中,用PL/SQL Dev连接远程数据库的时候老是弹出空白提示框,网上找了很久,解决方法也很多,可是就是没法解决我这种情况的. 出现这种问题,解决方法大概有这几种: ...
- Aladdin and the Flying Carpet
Aladdin and the Flying Carpet https://cn.vjudge.net/contest/288520#problem/C It's said that Aladdin ...