hdu 3879 Base Station 最大权闭合图

题目链接: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,权值为在此城市建立驿站的花费。
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<algorithm>
  7 #include<queue>
  8 #define inf 0x7fffffff
  9 using namespace std;
 10 const int maxn=55000+10;
 11 const int M = 25000000+10;
 12 
 13 int n,m,from,to;
 14 struct node
 15 {
 16     int v,flow;
 17     int next;
 18 }edge[M*2];
 19 int head[maxn],edgenum;
 20 
 21 void add(int u,int v,int flow)
 22 {
 23     edge[edgenum].v=v ;edge[edgenum].flow=flow;
 24     edge[edgenum].next=head[u] ;head[u]=edgenum++ ;
 25 
 26     edge[edgenum].v=u ;edge[edgenum].flow=0;
 27     edge[edgenum].next=head[v] ;head[v]=edgenum++ ;
 28 }
 29 
 30 int d[maxn];
 31 int bfs()
 32 {
 33     memset(d,0,sizeof(d));
 34     d[from]=1;
 35     queue<int> Q;
 36     Q.push(from);
 37     while (!Q.empty())
 38     {
 39         int u=Q.front() ;Q.pop() ;
 40         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 41         {
 42             int v=edge[i].v;
 43             if (!d[v] && edge[i].flow)
 44             {
 45                 d[v]=d[u]+1;
 46                 Q.push(v);
 47                 if (v==to) return 1;
 48             }
 49         }
 50     }
 51     return 0;
 52 }
 53 
 54 int dfs(int u,int flow)
 55 {
 56     if (u==to || flow==0) return flow;
 57     int cap=flow;
 58     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
 59     {
 60         int v=edge[i].v;
 61         if (d[v]==d[u]+1 && edge[i].flow)
 62         {
 63             int x=dfs(v,min(edge[i].flow,cap));
 64             edge[i].flow -= x;
 65             edge[i^1].flow += x;
 66             cap -= x;
 67             if (cap==0) return flow;
 68         }
 69     }
 70     return flow-cap;
 71 }
 72 
 73 int dinic()
 74 {
 75     int sum=0;
 76     while (bfs()) sum += dfs(from,inf);
 77     return sum;
 78 }
 79 
 80 int an[maxn];
 81 int main()
 82 {
 83     while (scanf("%d%d",&n,&m)!=EOF)
 84     {
 85         memset(head,-1,sizeof(head));
 86         edgenum=0;
 87         from=n+m+1;
 88         to=from+1;
 89         for (int i=1 ;i<=n ;i++)
 90         {
 91             scanf("%d",&an[i]);
 92             add(i,to,an[i]);
 93         }
 94         int a,b,c;
 95         int sum=0;
 96         for (int i=1 ;i<=m ;i++)
 97         {
 98             scanf("%d%d%d",&a,&b,&c);
 99             sum += c;
100             add(from,n+i,c);
101             add(n+i,a,inf);
102             add(n+i,b,inf);
103         }
104         printf("%d\n",sum-dinic());
105     }
106     return 0;
107 }

 

转载于:https://www.cnblogs.com/huangxf/p/4357852.html

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值