Maximum profit Max flow for "BZOJ-1497"

Source: Internet
Author: User

1497: [NOI2006] Maximum profit time limit:5 Sec Memory limit:64 MB
submit:3800 solved:1848
[Submit] [Status] [Discuss] Description

The new technology is hitting the mobile phone market, for the major operators, this is both an opportunity, but also a challenge. Thu Group's Cs&t Communications company in the new generation of communications technology on the eve of a bloody battle, need to do too much preparation, only to choose one site, you need to complete the preliminary market research, site survey, optimization and other projects. After the preliminary market survey and site survey, the company got a total of n can be used as a communication signal transfer station address, and due to the geographical differences of these addresses, the construction of communication stations in different places need to invest in the cost is not the same, Fortunately, after a preliminary investigation, these are known data: The cost of establishing the I Communication Hub is pi (1≤i≤n). In addition the company survey has drawn all the expected user groups, altogether m. Information about the I user base is summarized as AI, Bi, and CI: These users use broker AI and broker bi for communication, and the company can benefit from CI. (1≤i≤m, 1≤ai, Bi≤n) the cs&t Company of Thu Group can choose to set up a number of transit stations (input costs), provide services to some users and gain benefits. So how to choose the final station to make the company's net profit maximum? (Net Profit = sum of benefits-sum of input costs)

Input

The first line in the input file has two positive integers n and M. In the second row, there are n integers that describe the cost of establishing each communication station, in turn P1, P2, ..., PN. The following M-line, the three-digit AI, bi, and CI of the line (i + 2) describe the information for the I-user group. The meaning of all variables can be found in the topic description.

Output

Your program simply outputs an integer to the output file, indicating the maximum net profit a company can get.

Sample Input5 5
1 2 3) 4 5
1 2 3
2 3 4
1 3 3
1 4 2
4 5 3
Sample Output4
HINT

"Sample description" Select the establishment of 1, 2, 3rd, the transfer station, the cost of investment 6, profit is 10, so the maximum gain of 4. "Scoring method" is not part of the subject, your program output only and our answer exactly the same to obtain full marks, otherwise do not score. Data size and convention 80%: N≤200,m≤1 000. 100% of the data: N≤5 000,m≤50 000,0≤ci≤100,0≤pi≤100.

SourceSolution

A very obvious minimum cut model: Maximum weight closure sub-graph

If a B has a profit of the side of C, then create a new point, the point right is C, respectively to a A, a, a, a, a, B point right for their cost, so convert to the maximum right to close the sub-graph

Then the most powerful closed sub-graph model :

The edge in the original image, the capacity is INF

s to the positive point of the edge, the capacity is the point of the right

Negative weight point to T-edge, capacity is the opposite number of point right

The idea of a big question:

Assuming that all the positive weights are selected, then the cut in S is the deletion of a positive point, and the cut at t indicates a negative weight.

To minimize the right to delete and add the negative right to the smallest problem

So the answer is the right and the-minimum cut

Code
#include <cstdio>#include<cstring>#include<queue>using namespacestd;#defineINF 0x7fffffffintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;} structedgenode{intTo,cap,next;} edge[500010]; inthead[60010],cnt=1; intN,m,s,t,ans;intdis[60010],cur[60010]; voidAddintUintVintW) {cnt++;edge[cnt].to=v;edge[cnt].cap=w;edge[cnt].next=head[u];head[u]=CNT;} voidInsertintUintVintW) {Add (u,v,w); Add (V,u,0);} voidinit () {n=read (), m=read (); S=0; T=n+m+1; //memset (head,0xff,sizeof (head));     for(intX,i=1; i<=n;i++) {x=read ();     Insert (S,I,X); }      for(intU,v,w,i=1; i<=m;i++) {u=read (), V=read (), w=read (); Ans+=W; Insert (U,n+I,inf); Insert (V,n+I,inf); Insert (n+i,t,w); } } intq[600010<<1]; BOOLBFs () {memset (DIS,-1,sizeof(DIS)); q[1]=s; dis[s]=0; intHe=0, Ta=1;  while(he<ta) {             intnow=q[++He];  for(intI=head[now]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]==-1) dis[edge[i].to]=dis[now]+1, q[++ta]=edge[i].to; }    returndis[t]!=-1; } intDfsintLocintLow ) {     if(loc==t)returnLow ; intflow,cost=0;  for(intI=cur[loc]; I I=edge[i].next)if(dis[edge[i].to]==dis[loc]+1) {Flow=dfs (Edge[i].to,min (low-cost,edge[i].cap)); Edge[i].cap-=flow; edge[i^1].cap+=flow; if(EDGE[I].CAP) cur[loc]=i; Cost+=flow;if(Cost==low)returnLow ; }     if(!cost) dis[loc]=-1; returnCost ;} intDinic () {inttemp=0;  while(BFS ()) { for(intI=s; i<=t; i++) cur[i]=Head[i]; Temp+=DFS (S,inf); }     returntemp;} voidWork () {ans-=Dinic (); printf ("%d", ans); } intMain () {init ();     Work (); return 0; }

I did not want to do this problem. Help the next Yveh tune a The problem ... So, by the way, I changed a.

Maximum profit Max flow for "BZOJ-1497"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.