poj3249 Test for Job
Description
Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job, since there are swelling numbers of the unemployed. So some companies often use hard tests for their recruitment.
The test is like this: starting from a source-city, you may pass through some directed roads to reach another city. Each time you reach a city, you can earn some profit or pay some fee, Let this process continue until you reach a target-city. The boss will compute the expense you spent for your trip and the profit you have just obtained. Finally, he will decide whether you can be hired.
In order to get the job, Mr.Dog managed to obtain the knowledge of the net profit Vi of all cities he may reach (a negative Vi indicates that money is spent rather than gained) and the connection between cities. A city with no roads leading to it is a source-city and a city with no roads leading to other cities is a target-city. The mission of Mr.Dog is to start from a source-city and choose a route leading to a target-city through which he can get the maximum profit.
Input
The first line of each test case contains 2 integers n and m(1 ≤ n ≤ 100000, 0 ≤ m ≤ 1000000) indicating the number of cities and roads.
The next n lines each contain a single integer. The ith line describes the net profit of the city i, Vi (0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integers x, y indicating that there is a road leads from city x to city y. It is guaranteed that each road appears exactly once, and there is no way to return to a previous city.
Output
Sample Input
6 5
1
2
2
3
3
4
1 2
1 3
2 4
3 4
5 6
Sample Output
7
Hint
![](http://poj.org/images/3249_1.gif)
Source
1 program rrr(input,output); 2 const 3 inf=2000000000; 4 type 5 etype=record 6 t,next:longint; 7 end; 8 var 9 a,q,r,v,f:array[0..100010]of longint; 10 e:array[0..1000010]of etype; 11 n,m,i,x,y,h,t,ans:longint; 12 function max(a,b:longint):longint; 13 begin 14 if a>b then exit(a) else exit(b); 15 end; 16 procedure add(x,y:longint); 17 begin 18 e[i].t:=y;e[i].next:=a[x];a[x]:=i; 19 end; 20 begin 21 assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output); 22 while not eof do 23 begin 24 readln(n,m); 25 for i:=1 to n do readln(v[i]); 26 fillchar(a,sizeof(a),0);fillchar(r,sizeof(r),0); 27 for i:=1 to m do begin readln(x,y);inc(r[y]);add(x,y); end; 28 h:=0;t:=0; 29 for i:=1 to n do f[i]:=-inf; 30 ans:=-inf; 31 for i:=1 to n do if r[i]=0 then begin inc(t);q[t]:=i;f[i]:=v[i];if a[i]=0 then ans:=max(ans,v[i]); end; 32 while h<t do 33 begin 34 inc(h);i:=a[q[h]]; 35 while i<>0 do 36 begin 37 dec(r[e[i].t]);if r[e[i].t]=0 then begin inc(t);q[t]:=e[i].t; end; 38 f[e[i].t]:=max(f[e[i].t],f[q[h]]+v[e[i].t]); 39 if a[e[i].t]=0 then ans:=max(ans,f[e[i].t]); 40 i:=e[i].next; 41 end; 42 end; 43 writeln(ans); 44 end; 45 close(input);close(output); 46 end.