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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值