新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战。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 最大获利 - 最小割的更多相关文章

  1. BZOJ 1497: [NOI2006]最大获利 最小割

    1497: [NOI2006]最大获利 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1497 Description 新的技术正冲击着手 ...

  2. BZOJ 1497 最大获利(最大权闭合图)

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 4686  Solved: 2295 [Submit][Statu ...

  3. BZOJ1497: [NOI2006]最大获利[最小割 最大闭合子图]

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4375  Solved: 2142[Submit][Status] ...

  4. 【BZOJ1497】[NOI2006]最大获利 最小割

    裸的最小割,很经典的模型. 建图:要求总收益-总成本最大,那么将每条弧与源点相连,流量为成本,每个收益与汇点相连,流量为收益,然后每条弧与它所能到达的收益相连,流量为inf. 与源点相连的是未被选中的 ...

  5. [BZOJ 2127] happiness 【最小割】

    题目链接:BZOJ - 2127 题目分析 首先,每个人要么学文科,要么学理科,所以可以想到是一个最小割模型. 我们就确定一个人如果和 S 相连就是学文,如果和 T 相连就是学理. 那么我们再来确定建 ...

  6. BZOJ.3532.[SDOI2014]LIS(最小割ISAP 退流)

    BZOJ 洛谷 \(LIS\)..经典模型? 令\(f_i\)表示以\(i\)结尾的\(LIS\)长度. 如果\(f_i=1\),连边\((S,i,INF)\):如果\(f_i=\max\limits ...

  7. BZOJ 2561 最小生成树 | 网络流 最小割

    链接 BZOJ 2561 题解 用Kruskal算法的思路来考虑,边(u, v, L)可能出现在最小生成树上,就是说对于所有边权小于L的边,u和v不能连通,即求最小割: 对于最大生成树的情况也一样.容 ...

  8. BZOJ 1391: [Ceoi2008]order [最小割]

    1391: [Ceoi2008]order Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1509  Solved: 460[Submit][Statu ...

  9. BZOJ 1797: [Ahoi2009]Mincut 最小割

    1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2076  Solved: 885[Submit] ...

随机推荐

  1. 查看mobileprovision信息

    security cms -D -i ***********.mobileprovision

  2. Implicit declaration of function 'BMKCoordinateForMapPoint' is invalid in C99

    少一个头文件  #import <BaiduMapAPI_Utils/BMKGeometry.h> 加上这个就好了 <HPHalfScreenTopBar: 0x103570bb0; ...

  3. Jena 操作 RDF 文件

    1. RDF 入门 RDF(Resource Description Framework)是由W3C规定的,描述资源(resource)的数据模型(data model),: RDF 使用Web标识符 ...

  4. mybatis中大于等于、小于等于的写法

    在xml格式中,常常会遇到xml解析sql时候出错,这个时候需要用其他符号来表示.在mybatis中会遇到,需要做如下的转换:

  5. Linux输入输出重定向和文件查找值grep命令

    Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...

  6. 优化dedecms设置文章url自定义规则

    DEDECMS自定义URL规则的做得还是不错的,可清楚的看到URL中可用到的变量,并且这些变量包括年月日.时间戳.文章 ID.拼音+文章ID.拼音部首.栏目目录及日期加ID转换的字符串等,基本可以很好 ...

  7. C#基础笔记(第十天)

    C#基础笔记(第十天) 1.字段.属性.方法.构造函数字段:存储数据属性:保护字段,对字段的取值和设值进行限定方法:描述对象的行为构造函数:初始化对象(给对象的每个属性依次的赋值)类中成员,如果不加访 ...

  8. 飞跃平野(sdut1124)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1124 飞跃原野 Time Limit: 500 ...

  9. 机器学习理论基础学习3.2--- Linear classification 线性分类之线性判别分析(LDA)

    在学习LDA之前,有必要将其自然语言处理领域的LDA区别开来,在自然语言处理领域, LDA是隐含狄利克雷分布(Latent Dirichlet Allocation,简称LDA),是一种处理文档的主题 ...

  10. random随机数应用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...