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. 解决更新到os x10.11后openssl头文件无法找到的问题

    os x从10.10更新到10.11后,原有代码编译报错,#include <openssl/ssl.h>等头文件无法找到: "openssl/ssl.h: No such fi ...

  2. 解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法

    解决在安装Fiddler4.6版本后,在手机上安装证书出现的问题解决方法 设置fiddler抓手机包后,在手机上访问http://ip:port,出现如下问题: 问题:creation of the ...

  3. SQLServer查询耗时sql语句

    qs.total_worker_time/qs.execution_count as [Avg CPU Time], , ( ) as query_text, qt.dbid, dbname=db_n ...

  4. vue分环境打包配置方法一

    直接上代码配置: 首先是config下面的文件修改 dev.env.js  'use strict' const merge = require('webpack-merge') const prod ...

  5. 数学题 追及相遇—HDOJ1275 人傻需要多做题

    两车追及或相遇问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  6. caffe的调试技巧 和 使用split层

    1.网络中的layer层的输出,只要没有作为其他层的输入,caffe的日志就会把这个top输出(如果你用那个网站画网络结构图,你也会发现这种情况的层的颜色是不一样的,是紫色的) 2.如果你想看某一层在 ...

  7. 看paper的网址

    http://www.arxiv-sanity.com/ https://scirate.com/ google搜cvpr open access.iccv open access

  8. OpenCV2:介绍

    一.OpenCV简介 OpenCV所有的类和函数都在cv命名空间里面,可以用 using namespace cv; #include "opencv2/opencv.hpp" 1 ...

  9. HTTP协议重定向

    HTTP重定向:服务器无法处理浏览器发送过来的请求(request),服务器告诉浏览器跳转到可以处理请求的url上.(浏览器会自动访问该URL地址,以至于用户无法分辨是否重定向了.) 重定向的返回码3 ...

  10. shell脚本,按单词出现频率降序排序。

    [root@localhost oldboy]# cat file the squid project provides a number of resources toassist users de ...