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] ...
随机推荐
- Alpine Linux
Alpine Linux Docker镜像基于Alpine Linux操作系统,后者是一个面向安全的轻型Linux发行版.不同于通常Linux发行版,Alpine Linux采用了musl libc和 ...
- 新同事,git又报错Please move or remove them before you merge
http://blog.csdn.net/hufengvip/article/details/17231133 下面一行搞定 git clean -d -fx ""
- linux系统下添加计划任务执行php文件方法
在web开发过程中,经常需要设定一些定期执行的任务,比如商品定时上下架.我们以php文件为例,讲解linux下的计划任务,来看看吧 在web开发过程中,经常需要设定一些定期执行的任务,比如商品定时 ...
- JAVA队列的使用
JAVA队列的使用 今天跟大家来看看如何在项目中使用队列.首先我们要知道使用队列的目的是什么?一般情况下,如果是一些及时消息的处理,并且处理时间很短的情况下是不需要使用队列的,直接阻塞式的方法调用就可 ...
- easyUi引入方法
1:创建一个动态web工程: 去官网http://www.jeasyui.net/download/下载官网文档 我去官网下载的最新版本,个人根据自己的需求下载即可.2:在webConte ...
- Shell的>/dev/null、2>&1、2>1
转载自:http://dos2unix.cn/link/480 1. 标准输入stdin文件描述符为0,标准输出stdout文件描述符为1,标准错误stderr文件描述符为2 2. /dev/null ...
- LINUX的特殊字符含义
# 井号 (comments)这几乎是个满场都有的符号,除了先前已经提过的"第一行"#!/bin/bash井号也常出现在一行的开头,或者位于完整指令之后,这类情况表示符号后面的是注 ...
- HTML5插件
HTML 助手(插件) 辅助应用程序(helper application)是可由浏览器启动的程序.辅助应用程序也称为插件. 辅助程序可用于播放音频和视频(以及其他).辅助程序是使用 <obje ...
- java读取resource目录下的配置文件
java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取 / 代表resource目录 InputSt ...
- OCR学习及tesseract的一些测试
最近接触OCR,先收集一些资料,包括成熟软件.SDK.流行算法. 1. 一个对现有OCR软件及SDK的总结,比较全面,包括支持平台.编程语言.支持字体语言.输出格式.相关链接等 http://en.w ...