codevs1028 Flower shop window layout (fee flow)

Source: Internet
Author: User

These days just learned the cost stream, found this problem to practice a practiced hand.

Topic:

Title Description Description

Assuming the most beautiful way to decorate the shop window, there is an F bouquet, v vase, we use an aesthetic value (an integer) to represent each bouquet of flowers into each vase produced by the aesthetic effect. In order to achieve the best aesthetic effect, the flower must be placed to achieve the greatest aesthetic value.

Enter a description input Description

First act two integers f,v (f<=v<=100)

The next line of F is a line of v integers, and the number of lines I, J, indicates the aesthetic value of the first bundle of flowers placed in the J Vase.

outputs description output Description

An integer that is the maximum aesthetic value.

sample input to sample

2 2

10 0

3 T

Sample output Sample outputs

12

  The problem is clearly the maximum weight matching of the binary graph, which can be done with the maximum cost of the maximum flow. Practice: First build the diagram, at the source point to each bunch of flowers with a flow of 1, spending 0 of the points (only once per bouquet), in each flower screen to the meeting point a flow of 1, the cost of 0 of the edge (each vase can be used only once), and then in each bouquet and each vase with a flow of 1, The weights are the edges of this matching aesthetic value, and then the SPFA is used to find the augmented path, when the opposite edge is built, the flow of the opposite edge is the flow of this augmented path, and the cost is the opposite number of the original side cost.

At the beginning of the writing was affected by the maximum flow, as well as the maximum flow of the building of a hierarchical map, so WA two times also find no error. Later, the hierarchical map was deleted to AC. In fact, the role of the hierarchy is to avoid the ring caused by the cycle of death, and use SPFA to find the augmented road, it has avoided this problem, but will be the original image of some of the edge of the deletion, so the cost of the flow should not use a hierarchical map .

Code:

varA,c:Array[0.. About,0.. About] oflongint;//A is the original, and C is the cost figure fa,d:Array[0.. About] oflongint;//Fa[i] is in the shortest path to I on the previous point of I (precursor node), D[i] is the shortest path to I distance B:Array[0.. About] ofboolean;//whether the record is in the queue Q:Array[0..40010] oflongint;//queue N,m,i,j,k,p,t,h,sum:longint;procedureSPFA (s:longint);//SPFA TemplatesvarI,h,t:longint;begin   fori:=0  toN Do beginD[i]:=-1<< -; b[i]:=true;//InitializeEnd; H:=1; t:=1; q[1]:=s; d[s]:=0; B[s]:=false; fa[s]:=-1;//Initialize 2Repeat     fori:=0  toN Do      if(a[q[h],i]>0) and(D[q[h]]+c[q[h],i]>d[i]) Then begin//determine if there is an edge, whether better d[i]:=d[q[h]]+c[q[h],i]; fa[i]:=q[h];//Update DistanceifB[i] Then beginInc (T); Q[t]:=i; b[i]:=false;//QueueEnd; End; B[Q[H]]:=true; Inc (H);//out TeamuntilH>T;End;functionFlow (S,t:longint): Longint;varP,min:longint;beginSPFA (s); ifd[t]=-1<< -  ThenExit0);//determine if there is an augmented path P:=t; min:=1<< -;  whilefa[p]>=0  Do begin    ifMIN&GT;A[FA[P],P] Thenmin:=a[fa[p],p];//from the meeting point access to the source point, calculate the traffic p:=Fa[p]; End; P:=T;  whilefa[p]>=0  Do beginC[p,fa[p]]:=-c[fa[p],p];//build reverse Side 1 A[fa[p],p]:=a[fa[p],p]-min; a[p,fa[p]]:=a[p,fa[p]]+min;//build reverse Side 2 p:=Fa[p]; End; Sum:=sum+d[t];//Add the cost of this augmentation, update the answer exit (min);End;beginread (n,m);  fori:=1  toN Do begina[0, i]:=1; c[0, i]:=0;//build a diagram 1End;  fori:=1  toM Do beginA[i+n,n+m+1]:=1; c[i+n,n+m+1]:=0;//build a diagram 2End;  fori:=1  toN Do     forj:=1  toM Do beginread (k); A[i,n+j]:=1; c[i,n+j]:=k;//Build a diagram 3End; N:=n+m+1; sum:=0; k:=1;  whileK>0  DoK:=flow (0, n);//A line of cost flow writeln (sum);//output Maximum aesthetic valueEnd.
View Code

codevs1028 Flower shop window layout (fee flow)

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.