bzoj 1497 最大获利 - 最小割
新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前期市场研究、站址勘测、最优化等项目。在前期市场调查和站址勘测之后,公司得到了一共N个可以作为通讯信号中转站的地址,而由于这些地址的地理位置差异,在不同的地方建造通讯中转站需要投入的成本也是不一样的,所幸在前期调查之后这些都是已知数据:建立第i个通讯中转站需要的成本为Pi(1≤i≤N)。另外公司调查得出了所有期望中的用户群,一共M个。关于第i个用户群的信息概括为Ai, Bi和Ci:这些用户会使用中转站Ai和中转站Bi进行通讯,公司可以获益Ci。(1≤i≤M, 1≤Ai, Bi≤N) THU集团的CS&T公司可以有选择的建立一些中转站(投入成本),为一些用户提供服务并获得收益(获益之和)。那么如何选择最终建立的中转站才能让公司的净获利最大呢?(净获利 = 获益之和 - 投入成本之和)
Input
输入文件中第一行有两个正整数N和M 。第二行中有N个整数描述每一个通讯中转站的建立成本,依次为P1, P2, …, PN 。以下M行,第(i + 2)行的三个数Ai, Bi和Ci描述第i个用户群的信息。所有变量的含义可以参见题目描述。
Output
你的程序只要向输出文件输出一个整数,表示公司可以得到的最大净获利。
Sample Input
5 5
1 2 3 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3
Sample Output
4
Hint
【样例说明】选择建立1、2、3号中转站,则需要投入成本6,获利为10,因此得到最大收益4。【评分方法】本题没有部分分,你的程序的输出只有和我们的答案完全一致才能获得满分,否则不得分。【数据规模和约定】 80%的数据中:N≤200,M≤1 000。 100%的数据中:N≤5 000,M≤50 000,0≤Ci≤100,0≤Pi≤100。
当要从一个用户获得收益那么就得选择相应的中转站,很像最大权闭合图,于是思考按照它的方法来建图。
用户代表的点和源点连边,容量为对应的收益,中转站代表的点和汇点连边,容量为对应的成本。用户i需要哪两个中转站,点i就向这个中转站连边,容量为无限大。
稍微解释一下这个网络。考虑用户和源点之间的边。当它被割掉了,说明满足这个用户可能收益和成本(只是对于这个用户来说)相等,或者从他这儿得到收益结果会是亏本。如果它没有被割掉,容量和流量的差就是从它这儿得到的收益。
所以最终的答案是收益的总和 - 最小割容量 = 收益的总和 - 最大流流量
Code
/**
* bzoj
* Problem#1497
* Accepted
* Time:746ms
* Memory:14412k
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} template<typename T>class Matrix{
public:
T *p;
int lines;
int rows;
Matrix():p(NULL){ }
Matrix(int rows, int lines):lines(lines), rows(rows){
p = new T[(lines * rows)];
}
T* operator [](int pos){
return (p + pos * lines);
}
};
#define matset(m, i, s) memset((m).p, (i), (s) * (m).lines * (m).rows) typedef class Edge {
public:
int end;
int next;
int flow;
int cap;
Edge(int end = , int next = -, int flow = , int cap = ):end(end), next(next), flow(flow), cap(cap) { }
}Edge; typedef class MapManager {
public:
int ce;
vector<Edge> edge;
int* h; MapManager():ce(), h(NULL) { }
MapManager(int nodes):ce() {
h = new int[(const int)(nodes + )];
memset(h, -, sizeof(int) * (nodes + ));
} inline void addEdge(int from, int end, int flow, int cap) {
edge.push_back(Edge(end, h[from], flow, cap));
h[from] = ce++;
} inline void addDoubleEdge(int from, int end, int cap) {
if(cap == ) return;
addEdge(from, end, , cap);
addEdge(end, from, cap, cap);
} Edge& operator [] (int pos) {
return edge[pos];
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
#define m_endpos -1 typedef class Point {
public:
int x;
int y;
Point(int x = , int y = ):x(x), y(y) { }
}Point; int n, m;
int* p;
int *a, *b, *c;
MapManager g; int s, t; inline void init() {
readInteger(n);
readInteger(m);
p = new int[(const int)(n + )];
a = new int[(const int)(m + )];
b = new int[(const int)(m + )];
c = new int[(const int)(m + )];
for(int i = ; i <= n; i++)
readInteger(p[i]);
for(int i = ; i <= m; i++) {
readInteger(a[i]);
readInteger(b[i]);
readInteger(c[i]);
}
} int sum = ;
inline void init_map() {
s = , t = n + m + ;
g = MapManager(t);
for(int i = ; i <= m; i++) {
g.addDoubleEdge(s, i, c[i]);
g.addDoubleEdge(i, a[i] + m, inf);
g.addDoubleEdge(i, b[i] + m, inf);
sum += c[i];
}
for(int i = ; i <= n; i++)
g.addDoubleEdge(i + m, t, p[i]);
} int* dis;
boolean* vis;
queue<int> que;
inline boolean bfs() {
memset(vis, false, sizeof(boolean) * (t + ));
que.push(s);
vis[s] = true;
dis[s] = ;
while(!que.empty()) {
int e = que.front();
que.pop();
for(int i = m_begin(g, e); i != m_endpos; i = g[i].next) {
if(g[i].cap == g[i].flow) continue;
int eu = g[i].end;
if(vis[eu]) continue;
vis[eu] = true;
dis[eu] = dis[e] + ;
que.push(eu);
}
}
return vis[t];
} int *cur;
inline int blockedflow(int node, int minf) {
if((node == t) || (minf == )) return minf;
int f, flow = ;
for(int& i = cur[node]; i != m_endpos; i = g[i].next) {
int& eu = g[i].end;
if(dis[eu] == dis[node] + && g[i].flow < g[i].cap && (f = blockedflow(eu, min(minf, g[i].cap - g[i].flow))) > ) {
minf -= f;
flow += f;
g[i].flow += f;
g[i ^ ].flow -= f;
if(minf == ) return flow;
}
}
return flow;
} inline void init_dinic() {
vis = new boolean[(const int)(t + )];
dis = new int[(const int)(t + )];
cur = new int[(const int)(t + )];
} inline int dinic() {
int maxflow = ;
while(bfs()) {
for(int i = ; i <= t; i++)
cur[i] = m_begin(g, i);
maxflow += blockedflow(s, inf);
}
return maxflow;
} inline void solve() {
printf("%d\n", sum - dinic());
} int main() {
init();
init_map();
init_dinic();
solve();
return ;
}
bzoj 1497 最大获利 - 最小割的更多相关文章
- BZOJ 1497: [NOI2006]最大获利 最小割
1497: [NOI2006]最大获利 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1497 Description 新的技术正冲击着手 ...
- BZOJ 1497 最大获利(最大权闭合图)
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 4686 Solved: 2295 [Submit][Statu ...
- BZOJ1497: [NOI2006]最大获利[最小割 最大闭合子图]
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 4375 Solved: 2142[Submit][Status] ...
- 【BZOJ1497】[NOI2006]最大获利 最小割
裸的最小割,很经典的模型. 建图:要求总收益-总成本最大,那么将每条弧与源点相连,流量为成本,每个收益与汇点相连,流量为收益,然后每条弧与它所能到达的收益相连,流量为inf. 与源点相连的是未被选中的 ...
- [BZOJ 2127] happiness 【最小割】
题目链接:BZOJ - 2127 题目分析 首先,每个人要么学文科,要么学理科,所以可以想到是一个最小割模型. 我们就确定一个人如果和 S 相连就是学文,如果和 T 相连就是学理. 那么我们再来确定建 ...
- BZOJ.3532.[SDOI2014]LIS(最小割ISAP 退流)
BZOJ 洛谷 \(LIS\)..经典模型? 令\(f_i\)表示以\(i\)结尾的\(LIS\)长度. 如果\(f_i=1\),连边\((S,i,INF)\):如果\(f_i=\max\limits ...
- BZOJ 2561 最小生成树 | 网络流 最小割
链接 BZOJ 2561 题解 用Kruskal算法的思路来考虑,边(u, v, L)可能出现在最小生成树上,就是说对于所有边权小于L的边,u和v不能连通,即求最小割: 对于最大生成树的情况也一样.容 ...
- BZOJ 1391: [Ceoi2008]order [最小割]
1391: [Ceoi2008]order Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1509 Solved: 460[Submit][Statu ...
- BZOJ 1797: [Ahoi2009]Mincut 最小割
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2076 Solved: 885[Submit] ...
随机推荐
- CCCC L2-022. 重排链表
题解:直接list模拟,头尾两个指针,分别将头尾元素push到另一个list里面,输处输入方式同上一篇 坑:第一发卡了第二个样例,第二发卡了第4个,莫名其妙,所以把两个代码合起来,然后强行ac了. # ...
- Oracle体系结构之数据库启动的不同状态
数据库启动的不同状态: nomount状态:spfile和plile mount状态:control file open状态:data file和redo file 启动数据库的过程:nomount状 ...
- Oracle管理监控之linux下wendba登录设置
[root@localhost ~]# su - oracle [oracle@localhost ~]$ which jar/usr/bin/jar [oracle@localhost ~]$ ex ...
- iOS核心动画の摘记
- maven配置(myeclipse版)
使用环境说明: OS:windows 7 64位 java version: "jdk1.8.0_66" IDE:myeclipse 2017 1.下载 下载地址:http://m ...
- vue - vue-cli脚手架安装和webpack-simple模板项目生成
ue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. GitHub地址是:https://github.com/vuejs/vue-cli 一.安 ...
- 2018/04/16 PHP 设计模式之工厂模式
学习设计模式一定要知道它是为了什么而产生的,凡事一定有原因. 站在巨人的肩膀上学习,推荐两篇文章 什么是php工厂模式?为何要用php工厂模式? php工厂模式的使用实例总结 PHP设计模式之工厂模式 ...
- Python开发【数据结构】:字典内部剖析
字典内部剖析 开篇先提出几个疑问: 所有的类型都可以做字典的键值吗? 字典的存储结构是如何实现的? 散列冲突时如何解决? 最近看了一些关于字典的文章,决定通过自己的理解把他们写下来:本章将详细阐述上面 ...
- java 泛型没有协变类型, 所以要重用extends, 但使用List<? extends Fruit> 可以是ArrayList<Fruit>()、ArrayList<Apple>()、ArrayList<Orange>(), 因此不能add元素进去
class Fruit{} class Apple extends Fruit{} class SubApple extends Apple{} class Orange extends Fruit{ ...
- android 操作SD卡上的文件
(1)说明:操作SD卡上的文件须要增加下面权限 在SD卡上创建和删除文件权限 <uses-permission android:name="android.permission.M ...