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)