HDU 3879 Base Station
Base Station
This problem will be judged on HDU. Original ID: 3879
64-bit integer IO format: %I64d Java class name: Main
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
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
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的更多相关文章
- hdu 3879 Base Station 最大权闭合图
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...
- HDU 3879 Base Station(最大权闭合子图)
经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...
- HDU 3879 Base Station(最大权闭合子图)
将第i个用户和他需要的基站连边,转化成求二分图的最大权闭合子图. 答案=正权点之和-最小割. # include <cstdio> # include <cstring> # ...
- HDU 3897 Base Station (网络流,最大闭合子图)
题意:给定n个带权点m条无向带权边,选一个子图,则这个子图的权值为 边权和-点权和,求一个最大的权值. 析:把每条边都看成是一个新点,然后建图,就是一个裸的最大闭合子图. 代码如下: #pragma ...
- hdu3879 Base Station 最大权闭合子图 边权有正有负
/** 题目:hdu3879 Base Station 最大权闭合子图 边权有正有负 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 题意:给出n个 ...
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
hdu3879 base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 GSM Base Station Identification (点在多边形内模板)
In the Personal Communication Service systems such as GSM (Global System for Mobile Communications), ...
- HDU 3879 && BZOJ 1497:Base Station && 最大获利 (最大权闭合图)
http://acm.hdu.edu.cn/showproblem.php?pid=3879 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 ...
- hdu 4937 base进制只含3456的base数
http://acm.hdu.edu.cn/showproblem.php?pid=4937 给定一个数n,若这个数在base进制下全由3,4,5,6组成的话,则称base为n的幸运进制,给定n,求有 ...
随机推荐
- Flask 学习系列(一) -登录
Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 .Flask使用 BSD 授权. Flask也被称为 “ ...
- ES-什么是elasticsearch
前言 观今宜鉴古,无古不成今.在学习elasticsearch之前,我们要知道,elasticsearch是什么?为什么要学习elasticsearch?以及用它能干什么? 关于elasticsear ...
- 【详解】JS中的作用域、闭包和回收机制
在讲解主要内容之前,我们先来看看JS的解析顺序,我们惯性地觉得JS是从上往下执行的,所以我们要用一个变量来首先声明它,来看下面这段代码: alert(a); var a = 1; 大家觉得这段代码有什 ...
- vue从入门到开发--1-安装脚手架
一: 1.在文件目录下打开命令窗口(按住shift+右键[在此处打开命令窗口]或者直接ctrl+R打开命令窗口,利用cd选择到自己的文件目录) 2.$ npm install --global vue ...
- write命令
write——给用户发信息,以Ctrl+D保存结束 命令所在路径:/usr/bin/write 示例1: # write xiaohua 执行命令后可以输入需要发送的信息,如下: 同时xiaohua收 ...
- (十一)maven之安装nexus私服
安装nexus私服 前面的文章中对项目引入jar依赖包的时候,maven一般先是在本地仓库找对应版本的jar依赖包,如果在本地仓库中找不到,就上中央仓库中下载到本地仓库. 然而maven默认提供的中央 ...
- 怎样将英文版的Eclipse转为中文版的?
=====>1.打开eclipse储存文件夹 =====>2.在eclipse文件中找到dropins文件 =====>3.把已经下载好的eclipse汉化包复制到dropins中 ...
- vue2.0的变化
1. 在每个组件模板,不在支持片段代码 组件中模板: 之前: <template> <h3>我是组件</h3><strong>我是加粗标签</st ...
- 验证IP端与数据库Ip端是否重复!!!
select COUNT(id) from house_info_config hic where (hic.ip_start <![CDATA[<=]]> #{ipend} AND ...
- vs 2017 boost 安装目录 非安装
linuxg++ -Wall -std=c++11 boost_socks5.cpp -o boost_socks5 -lboost_system -lboost_thread -lpthread m ...