Poj3249test for Job (topology sort +DP)

Source: Internet
Author: User

Test instructions is to give a direction-free graph, each point has a weight, the point from the degree of 0 to the degree of the 0-point path through the point (including the start point) of the weight and the maximum value.

Analysis:

Note 3 points

1. There are multiple sets of data

2. May be a bit of weight is negative, that is, the result may be negative, the initial value should be set to negative infinity.

3. There is more than one point in degrees or out of 0.

Note that the above points are very simple, with the DP Dis[i]:=max (Dis[j],dis[i]+w[j]) in the topological sequencing process at the same time.

Practice topology sequencing and pointers before exams.

Code:

 Programtest;type Point=^node; Node=RecordData:longint;    Next:point; End;varA:Array[0..100000] ofPoint ; W,u,v,dis,f:Array[0..100000] ofInt64;  N,i,m,t,x,y,num:longint;  Ans:int64; P:point;functionMax (X,y:int64): Int64;begin  ifX>y ThenMax:=xElsemax:=y;End;begin   while   notEof Do  beginreadln (n,m);  fori:=1  toN Doreadln (W[i]);  fori:=1  toN DoA[i]:=Nil; T:=0; Ans:=-maxlongint; num:=0; Fillchar (u,sizeof (u),0); Fillchar (v,sizeof (v),0);  fori:=1  toM Do   beginreadln (x, y); New (P); P^.data:=y; P^.NEXT:=A[X]; a[x]:=p; U[y]:=u[y]+1; v[x]:=v[x]+1; End;  fori:=1  toN Dodis[i]:=-Maxlongint;  fori:=1  toN Do ifu[i]=0  Then beginInc (T); F[t]:=i; Dis[i]:=w[i];End; Repeatx:=f[t]; Dec (T); Inc (NUM); New (P); P:=A[x];  whileP<>Nil  Do     beginy:=p^.data; Dis[y]:=max (dis[y],dis[x]+W[y]);       Dec (U[y]); ifu[y]=0  Then        beginInc (T); F[t]:=y; End; P:=P^.next; End; untilnum=N;  fori:=1  toN Do   ifv[i]=0  Thenans:=Max (ans,dis[i]);  Writeln (ANS); End;End.
View Code

Poj3249test for Job (topology sort +DP)

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.