zoukankan      html  css  js  c++  java
  • 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 input file includes several test cases. 
    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 iVi (0 ≤ |Vi| ≤ 20000) 
    The next m lines each contain two integers xy 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

    The output file contains one line for each test cases, in which contains an integer indicating the maximum profit Dog is able to obtain (or the minimum expenditure to spend)

    Sample Input

    6 5
    1
    2
    2
    3
    3
    4
    1 2
    1 3
    2 4
    3 4
    5 6
    

    Sample Output

    7

    Hint

    Source

    POJ Monthly--2007.07.08, 落叶飞雪
     
     
    这题是拓扑排序+dp。只需注意几个细节:
    1)dp初值为-inf
    2)有些点是孤立的,也要算
    因为这第二点WA无数次
     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.
  • 相关阅读:
    洛谷P1877: [HAOI2012]音量调节(动态规划)
    hihocoder#1014 : Trie树
    hihocoder#1014 : Trie树
    河南省第六届大学生程序设计竞赛 : Card Trick(模拟)
    河南省第六届大学生程序设计竞赛 : Card Trick(模拟)
    第四届河南省程序设计大赛:表达式求值 (栈)
    第四届河南省程序设计大赛:表达式求值 (栈)
    第四届河南省程序设计大赛:BOBSLEDDING (贪心)
    第四届河南省程序设计大赛:BOBSLEDDING (贪心)
    菜根谭#52
  • 原文地址:https://www.cnblogs.com/Currier/p/6649299.html
Copyright © 2011-2022 走看看