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") ...
随机推荐
- 解决C3P0在Linux下Failed to get local InetAddress for VMID问题
解决C3P0在Linux下Failed to get local InetAddress for VMID问题 FailedtogetlocalInetAddressforVMID.Thisisunl ...
- scrapy爬虫的编写步骤
scrapy的步骤: a.编写item,爬取的各个属性 b.编写spider,name 要和 scrapy crawl xxspider一致,里面编写parse的信息,就是xpath获取item的各个 ...
- Java NIO Files
Java NIO Files Files.exists() Files.createDirectory() Files.copy() Overwriting Existing Files Files. ...
- NCBI之gene系列
1.基因系列中的data索引 2.基因ID之间的转换 对于生信,依托于别人的工具不如自己动手,由于研究发表的滞后性,往往很多工具提供的转换并不是最新的,况且开发者水平也参差不齐,理解原理才能让你来去自 ...
- java.lang.NoClassDefFoundError: net.tsz.afinal.FinalHttp
java.lang.NoClassDefFoundError: net.tsz.afinal.FinalHttpat com.hbjyjt.oa.utils.i.<init>(HttpRe ...
- J_link重刷固件
第一步:上电短接ERS 第二步:上电短接TST (擦除原来的固件) 完成上面两步后,再连接电脑,电脑将出现正常的串口连接 第三步:(烧固件) 在SAM-BA xxx固件烧写选好芯片类型,连接 ...
- Django项目的创建与管理和pycharm与Github的秘密
随笔 - 174 文章 - 21 评论 - 19 Django项目创建与管理 1.主题 这部分教程主要介绍如何通过Pycharm创建.管理.运行一个Django工程.对于Django模块的相关 ...
- cdh5.13.1 hadoop hdfs HA模式无法启动
经过观察日志发现,JN三个节点启动正常,只有NN节点启动时提示JN节点没有格式化 停止HDFS下面所有服务 先启动JN节点 然后启动一个NN节点,观察三个JN节点日志 发现其中一个节点的日志正常,没有 ...
- pta l2-13(红色警报)
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805063963230208 题意:给n个顶点,m条边,问每次删 ...
- Jmeter录制APP脚本
启动 jmeter.bat 在 Test Plan 下 添加 Thread Group 在 WorkBench 下 添加 HTTP(S) Test Script Recorder: 配置 Global ...