bzoj 1497 (maximum weight closed subgraph)

1497: [NOI2006] Maximum Profit

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

Description

New technologies are impacting the mobile communication market. For major operators, this is both an opportunity and a challenge. CS&T Communication Company, a subsidiary of THU Group, needs to do too much preparation work on the eve of the bloody battle of the new generation of communication technology. Just to select one site, it needs to complete the preliminary market research, site survey, optimization and other projects. After the preliminary market survey and site survey, the company has obtained a total of N addresses that can be used as communication signal transfer stations. Due to the geographical differences of these addresses, the cost of constructing communication transfer stations in different places is also different. Fortunately, these are known data after the preliminary investigation: the cost of establishing the i-th communication relay station is Pi (1≤i≤N). In addition, the company surveyed all the expected user groups, a total of M. The information about the i-th user group is summarized as Ai, Bi and Ci: these users will use the transfer station Ai and the transfer station Bi for communication, and the company can benefit from Ci. (1≤i≤M, 1≤Ai, Bi≤N) CS&T companies of THU Group can selectively establish some transfer stations (input cost) to provide services for some users and obtain benefits (sum of benefits). So how to choose the final established transfer station to maximize the company's net profit? (net profit = sum of benefits - sum of input costs)

Input

The first line in the input file has two positive integers N and M. There are N integers in the second line to describe the establishment cost of each communication transfer station, which are P1, P2, …, PN in sequence. The following M lines, the three numbers Ai, Bi and Ci in the (i + 2) line describe the information of the i-th user group. The meaning of all variables can be found in the title description.

Output

Your program simply outputs an integer to the output file that represents the maximum net profit the company can make.

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 

[Example description] If you choose to build transfer stations 1, 2, and 3, you need to invest 6 in cost and 10 in profit, so you can get the maximum benefit of 4. [Grading method] There are no partial marks for this question. You can get full marks only if the output of your program is completely consistent with our answer, otherwise no marks will be awarded. [Data scale and convention] In 80% of the data: N≤200, M≤1 000. In 100% of the data: N≤5 000, M≤50 000, 0≤Ci≤100, 0≤Pi≤100.

Positive Sum-Minimum Cut

Building a map: imagine the edge as a point and the weight of the edge is connected to the value of the s point and the t point. For the point represented by an edge, the weights of two points are respectively connected to INF

Then run the max flow with the edge weights and minus the min cut  

  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\n",sum);
105 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851927&siteId=291194637