1789 最大获利

2006年NOI全国竞赛

 时间限制: 2 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
 
 
题目描述 Description

新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是 挑战。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 Description

输入文件中第一行有两个正整数 N 和 M 。 第二行中有 N 个整数描述每一个通讯中转站的建立成本,依次为 P1, P2, …, PN 。 以下 M 行,第(i + 2)行的三个数 Ai, Bi和 Ci描述第 i 个用户群的信息。 所有变量的含义可以参见题目描述。

输出描述 Output Description

你的程序只要向输出文件输出一个整数,表示公司可以得到的最大净获利。

样例输入 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

数据范围及提示 Data Size & 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。

解题:最大权闭合子图

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
};
arc e[];
int head[maxn],d[maxn],cur[maxn];
int tot,S,T,n,m;
void add(int u,int v,int flow){
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
bool bfs(){
memset(d,-,sizeof(d));
d[S] = ;
queue<int>q;
q.push(S);
while(!q.empty()){
int u = q.front();
q.pop();
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
return d[T] > -;
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,a;
for(int &i = cur[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] == d[u] + &&(a=dfs(e[i].to,min(low,e[i].flow)))){
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
}
}
if(!tmp) d[u] = -;
return tmp;
}
int dinic(){
int ans = ;
while(bfs()){
memcpy(cur,head,sizeof(head));
ans += dfs(S,INF);
}
return ans;
}
int main() {
int u,v,w;
while(~scanf("%d %d",&n,&m)){
memset(head,-,sizeof(head));
S = tot = ;
T = n + m + ;
int ans = ;
for(int i = ; i <= n; ++i){
scanf("%d",&w);
add(i,T,w);
}
for(int i = ; i < m; ++i){
scanf("%d %d %d",&u,&v,&w);
add(n+i+,u,INF);
add(n+i+,v,INF);
add(S,n+i+,w);
ans += w;
}
printf("%d\n",ans - dinic());
}
return ;
}

最新文章

  1. Unable to load configuration. - Class: java.net.AbstractPlainSocketImpl
  2. [转]Handler MessageQueue Looper消息循环原理分析
  3. 关于ORACLE隐式转换后性能问题
  4. kail-linux 下载地址
  5. Apache、tomcat、Nginx常用配置合集
  6. js保留小数点后N位的方法介绍
  7. C# 之 读写文件
  8. BZOJ 1297 迷路(矩阵)
  9. 配置mybatis错误总结
  10. Nunit概要
  11. poj 3180 The Cow Prom(tarjan+缩点 easy)
  12. [LeetCode][Python]14: Longest Common Prefix
  13. JAVA课程设计+购物车 个人博客
  14. lua_cocos精灵的不断闪动
  15. STL常用查找算法介绍
  16. JDBC连接池之C3P0
  17. PWM实现ADC和DAC
  18. MySQL Backup mydumper
  19. Innodb引擎下mysql自身配置优化
  20. Oracle PLSQL Demo - 05.WHILE循环[WHILE LOOP]

热门文章

  1. 基于better-scroll封装一个上拉加载下拉刷新组件
  2. 2019-03-28 Python SQL 的注释
  3. ajax异步请求获取数据,实现滚动数字的效果。
  4. Android 混淆后的代码调试
  5. hdu 5335 Walk Out 搜索+贪心
  6. iOS 从各种效果图颜色标注生成 UIColor
  7. artTemplate的使用
  8. m_Orchestrate learning system---十九、局部变量和块变量是什么
  9. m_Orchestrate learning system---二十二、html代码如何变的容易
  10. 大家都能看懂的 canvas基础教程