zoukankan      html  css  js  c++  java
  • bzoj 1497(最大权闭合子图)

    1497: [NOI2006]最大获利

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 6410  Solved: 3099
    [Submit][Status][Discuss]

    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

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

    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

    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

    然后跑一遍最大流  用边权和减去最小割  

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<map>
      8 #include<set>
      9 #include<vector>
     10 #include<cstdlib>
     11 #include<string>
     12 typedef long long ll;
     13 typedef unsigned long long LL;
     14 using namespace std;
     15 const int INF=0x3f3f3f3f;
     16 const double pi=acos(-1.0);
     17 const double eps=0.00000001;
     18 const int N=60100;
     19 struct node{
     20     int to,next;
     21     int flow;
     22 }edge[N*10];
     23 int head[N];
     24 int dis[N];
     25 int tot;
     26 void init(){
     27     memset(head,-1,sizeof(head));
     28     tot=0;
     29 }
     30 void add(int u,int v,int flow){
     31     edge[tot].to=v;
     32     edge[tot].flow=flow;
     33     edge[tot].next=head[u];
     34     head[u]=tot++;
     35 
     36     edge[tot].to=u;
     37     edge[tot].flow=0;
     38     edge[tot].next=head[v];
     39     head[v]=tot++;
     40 }
     41 int BFS(int s,int t){
     42     queue<int>q;
     43     memset(dis,-1,sizeof(dis));
     44     q.push(s);
     45     dis[s]=0;
     46     while(q.empty()==0){
     47         int u=q.front();
     48         q.pop();
     49         for(int i=head[u];i!=-1;i=edge[i].next){
     50             int v=edge[i].to;
     51             if(dis[v]==-1&&edge[i].flow){
     52                 dis[v]=dis[u]+1;
     53                 q.push(v);
     54             }
     55         }
     56     }
     57     if(dis[t]==-1)return 0;
     58     return 1;
     59 }
     60 int DFS(int s,int t,int flow){
     61     if(s==t)return flow;
     62     int ans=0;
     63     for(int i=head[s];i!=-1;i=edge[i].next){
     64         int v=edge[i].to;
     65         if(edge[i].flow&&dis[v]==dis[s]+1){
     66             int f=DFS(v,t,min(flow-ans,edge[i].flow));
     67             edge[i].flow=edge[i].flow-f;
     68             edge[i^1].flow=edge[i^1].flow+f;
     69             ans=ans+f;
     70             if(flow==ans)return flow;
     71         }
     72     }
     73     if(ans==0)dis[s]=-1;
     74     return ans;
     75 }
     76 int Dinc(int s,int t){
     77     int flow=0;
     78     while(BFS(s,t)){
     79         flow+=DFS(s,t,INF);
     80     }
     81     return flow;
     82 }
     83 int main(){
     84     int n,m;
     85     scanf("%d%d",&n,&m);
     86     init();
     87     int s=0;
     88     int t=m+n+1;
     89     for(int i=1;i<=n;i++){
     90         int x;
     91         scanf("%d",&x);
     92         add(i,t,x);
     93     }
     94     int sum=0;
     95     for(int i=1;i<=m;i++){
     96         int u,v,w;
     97         scanf("%d%d%d",&u,&v,&w);
     98         sum=sum+w;
     99         add(s,i+n,w);
    100         add(i+n,u,INF);
    101         add(i+n,v,INF);
    102     }
    103     sum=sum-Dinc(s,t);
    104     printf("%d
    ",sum);
    105 }
  • 相关阅读:
    MySql常用函数积累
    常用的linux命令
    Java replace和replaceAll
    json常用操作
    import { Subject } from 'rxjs/Subject';
    applicationCache
    mongo
    Mongodb更新数组$sort操作符
    Mongodb更新数组$pull修饰符
    使用forever运行nodejs应用
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/9009678.html
Copyright © 2011-2022 走看看