• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
hyxzc
博客园    首页    新随笔    联系   管理    订阅  订阅

C++之路进阶——codevs1789(最大获利)

1789 最大获利

 

2006年NOI全国竞赛

 时间限制: 2 s
 空间限制: 128000 KB
 题目等级 : 大师 Master
 
 
 
题目描述 Description

新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是 挑战。THU 集团旗下的 CS&T 通讯公司在新一代通讯技术血战的前夜,需要做 太多的准备工作,仅就站址选择一项,就需要完成前期市场研究、站址勘测、最 优化等项目。 在前期市场调查和站址勘测之后,公司得到了一共 N 个可以作为通讯信号中 转站的地址,而由于这些地址的地理位置差异,在不同的地方建造通讯中转站需 要投入的成本也是不一样的,所幸在前期调查之后这些都是已知数据:建立第 i 个通讯中转站需要的成本为 Pi(1≤i≤N)。 另外公司调查得出了所有期望中的用户群,一共 M 个。关于第 i 个用户群的 信息概括为 Ai, Bi和 Ci:这些用户会使用中转站 Ai和中转站 Bi进行通讯,公司 可以获益 Ci。(1≤i≤M, 1≤Ai, Bi≤N) THU 集团的 CS&T 公司可以有选择的建立一些中转站(投入成本),为一些 用户提供服务并获得收益(获益之和)。那么如何选择最终建立的中转站才能让 公司的净获利最大呢?(净获利 = 获益之和 – 投入成本之和)

输入描述 Input Description

输入文件中第一行有两个正整数 N 和 M 。 第二行中有 N 个整数描述每一个通讯中转站的建立成本,依次为 P1, P2, …, PN 。 以下 M 行,第(i + 2)行的三个数 Ai, Bi和 Ci描述第 i 个用户群的信息。 所有变量的含义可以参见题目描述。

输出描述 Output Description

你的程序只要向输出文件输出一个整数,表示公司可以得到的最大净获利。

样例输入 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

数据范围及提示 Data Size & Hint

选择建立 1、2、3 号中转站,则需要投入成本 6,获利为 10,因此得到最大 收益 4。

80%的数据中:N≤200,M≤1 000。

100%的数据中:N≤5 000,M≤50 000,0≤Ci≤100,0≤Pi≤100。 

题解:

   将S与中转站连边,权值为花费。

   将用户群与T连边,权值为收益。

   将用户群与中转站连边,权值为INF

   跑最小割。

代码:

   

#include<cstdio>
#include<queue>
#include<iostream>
#include<algorithm>
#define maxn 60010
#define INF 0x7fffffff
#define S 0
#define T n+1+m 

using namespace std;

int head[maxn],dis[55010],n,m,cnt=1,ans;
struct ss
   {
       int to;
       int next;
       int edge;
   }e[50000<<3];
   
void add(int u,int v,int w)
   {
        e[++cnt].to=v;
        e[cnt].next=head[u];
        e[cnt].edge=w;
        head[u]=cnt;
   }

void insert(int u,int v,int w)
   {
        add(u,v,w);
        add(v,u,0);
   }
    
bool bfs()
    {
      for (int i=1;i<=T;i++) dis[i]=INF;
      dis[S]=0;
      queue<int>que;
      que.push(S);
      while (!que.empty())    
        {
           int now=que.front();    
           que.pop();    
           for (int i=head[now];i;i=e[i].next)    
              if (e[i].edge&&dis[e[i].to]>dis[now]+1)
                 {
                      dis[e[i].to]=dis[now]+1;
                      que.push(e[i].to);
                      if (e[i].to==T) return 1;
                  }
        }
      return 0;    
     }
                
int dfs(int x,int inf)
     {
         if (x==T) return inf; 
         int rest=inf;
         for (int i=head[x];i&&rest;i=e[i].next)
            if (e[i].edge&&dis[e[i].to]==dis[x]+1)
               {
                   int now=dfs(e[i].to,min(e[i].edge,rest));
                   if (!now) dis[now]=0;
                   e[i].edge-=now;
                   e[i^1].edge+=now;
                   rest-=now;
               }
       return inf-rest;           
      } 
                
void dinic()
   {
        while (bfs()) ans+=dfs(S,INF);
   }

int main()
   {
        scanf("%d%d",&n,&m);
        for (int i=1;i<=n;i++)
          {
                int x;
                scanf("%d",&x);
                insert(S,i,x);
          }
    int absans=0;      
    for (int i=1;i<=m;i++)       
          {
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                absans+=w; 
          insert(u,n+i,INF);
                insert(v,n+i,INF);
                insert(n+i,T,w);
          }      
       dinic();
       printf("%d\n",absans-ans);
    return 0;
   }

 

 

posted @ 2016-02-23 20:33  hyxzc  阅读(575)  评论(3)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3