POJ 2175 Evacuation Plan 费用流 负圈定理
题目给了一个满足最大流的残量网络,判断是否费用最小。
如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了。
1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在负环上。
2.这个负环可能包括汇点t,所以构建残量网络的时候也要考虑防空洞到t上的容量。
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else freopen("in.txt","r",stdin);
// freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
} const int maxn = ;
int X[maxn], Y[maxn], B[maxn];
int P[maxn], Q[maxn], C[maxn];
int E[maxn][maxn];
int N, M; bool input()
{
if(scanf("%d%d", &N, &M) == EOF) return false;
for(int i = ; i <= N; i++)
scanf("%d%d%d", &X[i], &Y[i], &B[i]);
for(int i = ; i <= M; i++)
scanf("%d%d%d", &P[i], &Q[i], &C[i]);
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
scanf("%d", &E[i][j]);
return true;
} struct Edge
{
int from, to, cost, cap;
};
const int maxv = maxn * ;
int n,s,t;
vector<int> g[maxv];
vector<Edge> edge;
int road[maxv];
int d[maxv];
int inq[maxv];
int vcnt[maxv]; int SPFA()
{
queue<int> q;
memset(d, INF, sizeof(d));
memset(inq, false, sizeof(inq));
memset(vcnt, , sizeof(vcnt));
d[s] = ;
road[s] = -;
q.push(s); while(!q.empty())
{
int u = q.front(); q.pop();
inq[u] = false;
for(int i = ; i < g[u].size(); i++)
{
Edge &e = edge[g[u][i]];
if(e.cap > && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
road[e.to] = g[u][i];
if(!inq[e.to])
{
inq[e.to] = true;
if(++vcnt[e.to] > n) return e.to;
q.push(e.to);
}
}
}
}
return -;
}
void add(int from, int to, int cost, int cap, int flow)
{
edge.push_back((Edge){from, to, cost, cap - flow});
g[from].push_back(edge.size() - );
edge.push_back((Edge){to, from, -cost, flow});
g[to].push_back(edge.size() - );
} int Cost(int i, int j)
{
return abs(X[i] - P[j]) + abs(Y[i] - Q[j]) + ;
} void init()
{
for(int i = ; i <= n; i++)
g[i].clear();
edge.clear();
}
void construct()
{
n = N + M + ;
s = n - ;
t = n;
init(); int sum[maxn] = {};
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
sum[j] += E[i][j];
for(int i = ; i <= N; i++)
add(s, i, , B[i], );
for(int j = ; j <= M; j++)
add(j + N, t, , C[j], sum[j]);
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
add(i, j + N, Cost(i, j), min(B[i], C[i]), E[i][j]);
} int vis[maxv];
void solve()
{
int u = SPFA();
if(u == -)
{
printf("OPTIMAL\n");
return ;
} memset(vis, , sizeof(vis));
while()
{
if(vis[u]) break;
vis[u] = true;
u = edge[road[u]].from;
} memset(vis, , sizeof(vis));
for(int e = road[u]; !vis[edge[e].to]; e = road[edge[e].from])
{
int x = edge[e].from;
int y = edge[e].to;
vis[y] = true;
if(x == t || y == t) continue;
if(x < y) E[x][y - N] += ;
else E[y][x - N] -= ;
} printf("SUBOPTIMAL\n");
for(int i = ; i <= N; i++)
for(int j = ; j <= M; j++)
printf("%d%c", E[i][j], j == M? '\n': ' ');
}
int main()
{
debug();
while(input())
{
construct();
solve();
}
return ;
}
POJ 2175 Evacuation Plan 费用流 负圈定理的更多相关文章
- POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)
http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- POJ.2175.Evacuation Plan(消圈)
POJ \(Description\) \(n\)个建筑物,每个建筑物里有\(a_i\)个人:\(m\)个避难所,每个避难所可以容纳\(b_i\)个人. 给出每个建筑物及避难所的坐标,任意两点间的距离 ...
- poj 2175 Evacuation Plan 最小费用流判定,消圈算法
题目链接 题意:一个城市有n座行政楼和m座避难所,现发生核战,要求将避难所中的人员全部安置到避难所中,每个人转移的费用为两座楼之间的曼哈顿距离+1,题目给了一种方案,问是否为最优方案,即是否全部的人员 ...
- POJ - 2175 Evacuation Plan (最小费用流消圈)
题意:有N栋楼,每栋楼有\(val_i\)个人要避难,现在有M个避难所,每个避难所的容量为\(cap_i\),每个人从楼i到避难所j的话费是两者的曼哈顿距离.现在给出解决方案,问这个解决方案是否是花费 ...
- POJ2175:Evacuation Plan(消负圈)
Evacuation Plan Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 5665Accepted: 1481Special J ...
- POJ 2175 Evacuation Plan
Evacuation Plan Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Origina ...
- Codeforces Gym 100002 E "Evacuation Plan" 费用流
"Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- POJ 3680 Intervals(费用流+负权优化)
[题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...
- POJ 2175:Evacuation Plan(费用流消圈算法)***
http://poj.org/problem?id=2175 题意:有n个楼,m个防空洞,每个楼有一个坐标和一个人数B,每个防空洞有一个坐标和容纳量C,从楼到防空洞需要的时间是其曼哈顿距离+1,现在给 ...
随机推荐
- mac 10.9开启有道词典取词功能
取词时候,有道词典给出提示,说要去开启辅助功能,但提示的是在mac 10.8上面怎么操作,在10.9的话,就是以下位置去改了. 补充以下: 在mac机器上,实际上大多数的单词都能从自带的词典中查找到. ...
- ThinkPad E440 Ubuntu 13.1无线网卡 RTL8723BE 驱动解决办法总结
方法一.在使用网线链接到情况下 第一步.执行下边到命令,即从github上下载驱动程序并安装 sudo apt-get install linux-headers-generic build-esse ...
- 用C#抓取AJAX页面的内容
现在的网页有相当一部分是采用了AJAX技术,不管是采用C#中的WebClient还是HttpRequest都得不到正确的结果,因为这些脚本是在服务器发送完毕后才执行的! 但我们用IE浏览页面时是正常的 ...
- 使用 Jmeter 做 Web 接口测试
接口测试概述 定义 API testing is a type of software testing that involves testing application programming in ...
- Codeforces 723c [贪心][乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一个n和m. 第二行给n个数. 每次操作可以把n个数中的任何一个数替代为别的数,问最少的操作次数使得1.2.3.4.5...m中的数出现的次数 ...
- XE6移动开发环境搭建之IOS篇(8):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 安装PAServer ...
- finder的隐藏文件&IOS虚拟机地址
在终端里输入下面命令即可让它们显示出来. defaults write com.apple.finder AppleShowAllFiles -bool true 如果想恢复隐藏,可以用这个命令: ...
- nodejs生成UID(唯一标识符)——node-uuid模块
unique identifier 惟一标识符 -->> uid 在项目开发中我们常需要给某些数据定义一个唯一标识符,便于寻找,关联. node-uuid模块很好的提供了这个 ...
- express html模板项目搭建
初学express的亲们,估计要弄ejs和jade会比较烦躁,那就先html开始,简单笔记如下: 1.新建项目文件夹demotest 2.进入demotest >express -e ...
- Timus Online Judge 1001. Reverse Root
Input The input stream contains a set of integer numbers Ai (0 ≤ Ai ≤ 1018). The numbers are separat ...