题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879

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.

 
题意描述:有n个城市,在城市 i 建立新的驿站的花费为Pi,如果两个城市都有驿站则可以互相通信交流并且公司会因此获取到利益,比如A城市和B城市相互通信,那么可以获取到C的利益。问最大利益是多少。
算法分析:最大权闭合图。把两个城市相互通信的这条边看作点,比如A B C(如题意描述中解释),A和B的这条边看作点U,连边from到U,权值为C,连边U到A、U到B,权值为无穷大,连边A到to,B到to,权值为在此城市建立驿站的花费。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#define inf 0x7fffffff
using namespace std;
const int maxn=+;
const int M = +; int n,m,from,to;
struct node
{
int v,flow;
int next;
}edge[M*];
int head[maxn],edgenum; void add(int u,int v,int flow)
{
edge[edgenum].v=v ;edge[edgenum].flow=flow;
edge[edgenum].next=head[u] ;head[u]=edgenum++ ; edge[edgenum].v=u ;edge[edgenum].flow=;
edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
} int d[maxn];
int bfs()
{
memset(d,,sizeof(d));
d[from]=;
queue<int> Q;
Q.push(from);
while (!Q.empty())
{
int u=Q.front() ;Q.pop() ;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (!d[v] && edge[i].flow)
{
d[v]=d[u]+;
Q.push(v);
if (v==to) return ;
}
}
}
return ;
} int dfs(int u,int flow)
{
if (u==to || flow==) return flow;
int cap=flow;
for (int i=head[u] ;i!=- ;i=edge[i].next)
{
int v=edge[i].v;
if (d[v]==d[u]+ && edge[i].flow)
{
int x=dfs(v,min(edge[i].flow,cap));
edge[i].flow -= x;
edge[i^].flow += x;
cap -= x;
if (cap==) return flow;
}
}
return flow-cap;
} int dinic()
{
int sum=;
while (bfs()) sum += dfs(from,inf);
return sum;
} int an[maxn];
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(head,-,sizeof(head));
edgenum=;
from=n+m+;
to=from+;
for (int i= ;i<=n ;i++)
{
scanf("%d",&an[i]);
add(i,to,an[i]);
}
int a,b,c;
int sum=;
for (int i= ;i<=m ;i++)
{
scanf("%d%d%d",&a,&b,&c);
sum += c;
add(from,n+i,c);
add(n+i,a,inf);
add(n+i,b,inf);
}
printf("%d\n",sum-dinic());
}
return ;
}

hdu 3879 Base Station 最大权闭合图的更多相关文章

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

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

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

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

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

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

  4. HDU 3879 Base Station

    Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

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

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

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

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

  7. hdu 3879 最大权闭合图(裸题)

    /* 裸的最大权闭合图 解:参见胡波涛的<最小割模型在信息学竞赛中的应用 #include<stdio.h> #include<string.h> #include< ...

  8. hdu 3061 Battle 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3061 由于小白同学近期习武十分刻苦,很快被晋升为天策军的统帅.而他上任的第一天,就面对了一场极其困难的 ...

  9. hdu 3061 hdu 3996 最大权闭合图 最后一斩

    hdu 3061 Battle :一看就是明显的最大权闭合图了,水提......SB题也不说边数多少....因为开始时候数组开小了,WA....后来一气之下,开到100W,A了.. hdu3996. ...

随机推荐

  1. kvm介绍

    KVM(Kernel-Based Virtual Machines)是一个基于Linux内核的虚拟化技术, 可以直接将Linux内核转换为Hypervisor(系统管理程 序)从而使得Linux内核能 ...

  2. activity切换动画特效

    效果图: 结构图: 测试代码: 布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayou ...

  3. leetcode刷题笔记

    (1)Best Time to Buy and Sell Stock Total Accepted: 10430 Total Submissions: 33800My Submissions Say ...

  4. JavaScrip拖动动画中的常见BUG

    经常我们在用JS辛苦写完一个拖动效果之后 ,发现有各种无法用JS解决的BUG.比如拖动时DOM元素中的内容会变蓝,鼠标的指示会变为一个小+号,或disable的样式,通常这种情况一发生,我们的拖动效果 ...

  5. 成长记录 if语句输出 由大到小的数字

    #include<stdio.h> void main() { float a,b,c,d,e,f,g,t; scanf("%f,%f,%f,%f,%f,%f,%f", ...

  6. 菜鸟学习Hibernate——缓存

    Hibernate的缓存分为三种:一级缓存.二级缓存.查询缓存.下面我就为大家介绍一下. 一.概念. 一级缓存:第一级存放于session中称为一级缓存.Session 级别的缓存,它同session ...

  7. ThinkPHP3.2.2中开启REWRITE模式

    1. 在项目配置文件(\Application\Common\Conf\config.php)中配置URL模式 <?php return array( //URL模式 , ); 2. 在Thin ...

  8. MySql 用户 及权限操作

    bin/msyql -h host -u user -p    bin/mysql -u mysql -p  本地登录 如无密码按回车直接进入mySql   bin/mysqladmin -u roo ...

  9. hdu 4593 Robot

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4593 Robot Description A robot is a mechanical or vir ...

  10. PHP(一)

    最近一段时间一直忙于新版本的开发工作,所以虽然自己脑中有一些想法,但是苦于没有足够的时间去写下来.好了,昨天终于将大体的功能开发完成,时间上面也不会那么的紧张了.下来我想要好好的梳理一下,自己最近一段 ...