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,求有 ...
随机推荐
- 再谈Android AsyncTask的优缺点
导语:之前做习惯了Framework层的开发,今天在武汉斗鱼公司面试APP客户端的开发,其中一道题是讲述Asynctask的优缺点,我靠,我只是知道有这么一个东西,会用而已,看来之前的生活太过于安逸, ...
- 关于父类中的this指针的问题
在处理一个消息推送的问题的时候遇到个小问题,比如A是B的子类,当A生成实例时,会执行父类的构造函数,那么在父类中,this会是什么类型呢? 于是做了个小测试 子类ChildClass: public ...
- C#入门(2)
C#入门(2) Exception 基本异常的核心成员: System.Exception Property Meaning Data read-only,实现了IDirectory接口的一些键值对, ...
- 简单shell执行脚本
#!/bin/bash source /etc/profile APPLICATIONS_HOME="/opt/cpic_analy" APPLICATION_NAME=" ...
- poj1338 Ugly Numbers 打表, 递推
题意:一个数的质因子能是2, 3, 5, 那么这个数是丑数. 思路: 打表或者递推. 打表: 若该数为丑数,那么一定能被2 或者3, 或者5 整除, 除完之后则为1. #include <ios ...
- 【page-monitor 前端自动化 中篇】 源码分析
转载文章:来源(靠谱崔小拽) 上篇中初探了page-monitor的一些功能和在前端自动化测试方面的可行性,本篇主要分析下page-monitor的实现方式和源码. mode-module简介 pag ...
- Python基础篇 -- 小数据池和再谈编码
小数据池 1. id() 通过id()可以查看到一个变量表示的值在内存中的地址 s = "Agoni" print(id(s)) # 2410961093272 2. is 和 = ...
- shell脚本,计算学生分数的题目。
1.计算学生平均分数的值是多少? 2.计算每门课都大于80分的学生姓名.3.计算每门课都小于90分的学生姓名.
- -bash: xx: command not found 在有yum源情况下处理
-bash: xx: command not found 在有yum源情况下处理 yum provides "*/xx" ###"xx"代表某命令 或者 yu ...
- UIScrollView和MultiThreading、UITextField、Keyboard