Base Station

Time Limit: 2000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 3879
64-bit integer IO format: %I64d      Java class name: Main

A famous mobile communication company is planning to build a new set of base stations. According to the previous investigation, n places are chosen as the possible new locations to build those new stations. However, the condition of each position varies much, so the costs to built a station at different places are different. The cost to build a new station at the ith place is Pi (1<=i<=n).

When complete building, two places which both have stations can communicate with each other.

Besides,
according to the marketing department, the company has received m
requirements. The ith requirement is represented by three integers Ai, Bi and Ci, which means if place Ai and Bi can communicate with each other, the company will get Ci profit.

Now,
the company wants to maximize the profits, so maybe just part of the
possible locations will be chosen to build new stations. The boss wants
to know the maximum profits.

Input

Multiple test cases (no more than 20), for each test case:
The first line has two integers n (0<n<=5000) and m (0<m<=50000).
The second line has n integers, P1 through Pn, describes the cost of each location.
Next m line, each line contains three integers, Ai, Bi and Ci, describes the ith requirement.

Output

One integer each case, the maximum profit of the company.

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

Source

 
解题:最大权闭合子图
 #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = ;
struct arc{
int to,flow,next;
arc(int x = ,int y = ,int z = -){
to = x;
flow = y;
next = z;
}
}e[];
int head[maxn],d[maxn],gap[maxn],tot,S,T;
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++;
}
queue<int>q;
void bfs(){
for(int i = ; i <= T; ++i){
d[i] = -;
gap[i] = ;
}
d[T] = ;
q.push(T);
while(!q.empty()){
int u = q.front();
q.pop();
++gap[d[u]];
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] == -){
d[e[i].to] = d[u] + ;
q.push(e[i].to);
}
}
}
}
int dfs(int u,int low){
if(u == T) return low;
int tmp = ,minH = T - ;
for(int i = head[u]; ~i; i = e[i].next){
if(e[i].flow && d[e[i].to] + == d[u]){
int a = dfs(e[i].to,min(low,e[i].flow));
e[i].flow -= a;
e[i^].flow += a;
low -= a;
tmp += a;
if(!low) break;
if(d[S] >= T) return tmp;
}
if(e[i].flow) minH = min(minH,d[e[i].to]);
}
if(!tmp){
if(--gap[d[u]] == ) d[S] = T;
++gap[d[u] = minH + ];
}
return tmp;
}
int sap(int ret = ){
bfs();
while(d[S] < T) ret += dfs(S,INF);
return ret;
}
int main(){
int n,m,u,v,w;
while(~scanf("%d%d",&n,&m)){
memset(head,-,sizeof head);
int sum = tot = ;
S = n + m + ;
T = S + ;
for(int i = ; i <= n; ++i){
scanf("%d",&w);
add(S,i,w);
}
for(int i = ; i <= m; ++i){
scanf("%d%d%d",&u,&v,&w);
sum += w;
add(u,i + n,INF);
add(v,i + n,INF);
add(i + n,T,w);
}
printf("%d\n",sum-sap());
}
return ;
}

HDU 3879 Base Station的更多相关文章

  1. hdu 3879 Base Station 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...

  2. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  3. HDU 3879 Base Station(最大权闭合子图)

    将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...

  4. HDU 3897 Base Station (网络流,最大闭合子图)

    题意:给定n个带权点m条无向带权边,选一个子图,则这个子图的权值为 边权和-点权和,求一个最大的权值. 析:把每条边都看成是一个新点,然后建图,就是一个裸的最大闭合子图. 代码如下: #pragma ...

  5. hdu3879 Base Station 最大权闭合子图 边权有正有负

    /** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...

  6. hdu 3879 hdu 3917 构造最大权闭合图 俩经典题

    hdu3879  base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...

  7. 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)

    In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...

  8. HDU 3879 && BZOJ 1497:Base Station && 最大获利 (最大权闭合图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3879 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 ...

  9. hdu 4937 base进制只含3456的base数

    http://acm.hdu.edu.cn/showproblem.php?pid=4937 给定一个数n,若这个数在base进制下全由3,4,5,6组成的话,则称base为n的幸运进制,给定n,求有 ...

随机推荐

  1. MyBatis框架的XML数据访问Dao层接口的组合使用

    MyBatis 的前生为Apache的开源项目iBatis.其优势在于灵活,几乎可以替代JDBC,同时提供了编程接口.目前MyBatis的数据访问Dao层不需要实现类,也不需要像JDBC那样拼接Hql ...

  2. 五、UML类图和六大原则-----《大话设计模式》

    一.单一职责原则     就一个类而言,应该仅有一个引起它变化的原因.     如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力.这种耦合 ...

  3. Mysql order by 多字段排序

    mysql单个字段降序排序: select * from table order by id desc; mysql单个字段升序排序: select * from table order by id ...

  4. hdu6376 度度熊剪纸条

    思路: 01背包.有些细节需要注意一下,比如k = 0的情况. 实现: #include <bits/stdc++.h> using namespace std; typedef pair ...

  5. Vivado增量式编译

    Vivado 中的增量设计会重新利用已有的布局布线数据来缩短运行时间,并生成可预测的结果.当设计有 95% 以上的相似度时,增量布局布线的运行时间会比一般布局布线平均缩短2倍.若相似度低于80%,则使 ...

  6. COGS 930. [河南省队2012] 找第k小的数

    题目描述 看到很短的题目会让人心情愉悦,所以给出一个长度为N的序列A1,A2,A3,...,AN, 现在有M个询问,每个询问都是Ai...Aj中第k小的数等于多少. 输入格式 第一行两个正整数N,M. ...

  7. Cscope的使用(领略Vim + Cscope的强大魅力)

    文章出处:http://blog.csdn.net/dengxiayehu/article/details/6330200 Cscope的使用(领略Vim + Cscope的强大魅力) 1.Cscop ...

  8. 洛谷 P3958 奶酪

    谨以此题来纪念我爆炸的NOIp2017 这个题虽然很多人说是并查集,但是搜索也是毫无压力的,考场搜索细节写挂,爆了个不上不下的80分.今天无意看到这道题,终于AC 首先这道题要考虑一下精度问题,虽然出 ...

  9. C# IsNullOrEmpty与IsNullOrWhiteSpace

    IsNullOrEmpty:非空非NULL判断 IsNullOrWhiteSpace:非空非NULL非空格判断 后者优于前者 if (!string.IsNullOrWhiteSpace(valueE ...

  10. 八:SQL之DQL数据查询语言单表操作

    前言: DQL数据库查询语言是我们在开发中最常使用的SQL,这一章总结了单表操作部分的常用查询方式 主要操作有:查询所有字段.查询指定字段.查询指定记录.带IN的关键字查询,范围查询,陪查询.查询空值 ...