POJ3249 Test for job

 
POJ 3249 -- Test for job
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 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

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

POJ3249 Test for job

Source

 
 
解题报告:

Dag上的最长路径,用 拓扑排序,一开始把入度为0的点加入队列,之后不断枚举,知道队列为空;

注意 : val有可能为负,所以初始化dis为-inf,所以记录出度,scanf输入;

 1 /* QYP kuai wo dai ma*/
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<iomanip>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<cstdio>
 8 #include<queue>
 9 #include<ctime>
10 #include<cmath>
11 #include<stack>
12 #include<map>
13 #include<set>
14 #define rep(i,a,b) for(register int i=a;i<=b;i++)
15 #define ll long long
16 #define re register
17 #define inf 1<<29
18 using namespace std;
19 const int N=100010,M=1000010;
20 struct Edge{
21     int to,net;
22 }e[M];
23 int in[N],d[N],val[N],n,m;
24 int C[N];
25 int head[N],num_e;
26 inline int gi() {
27     re int res=0;
28     char ch=getchar();
29     while(ch<'0'||ch>'9') ch=getchar();
30     while(ch>='0'&&ch<='9') res=res*10+ch-'0',ch=getchar();
31     return res;
32 }
33 inline void topsort() {
34     queue<int>q;
35     rep(i,1,n) if(!in[i]) q.push(i),d[i]=val[i];
36     while(!q.empty()) {
37         int u=q.front();q.pop();
38         for(int i=head[u];i;i=e[i].net) {
39             int to=e[i].to;
40             if(d[to]<d[u]+val[to]) d[to]=d[u]+val[to];
41             if(--in[to]==0) q.push(to);
42         }
43     }
44 }
45 inline void add(int x,int y) {
46     e[++num_e].to=y,e[num_e].net=head[x],head[x]=num_e;
47 }
48 int main() {
49     freopen("Input.in","r",stdin);
50     freopen("Output.out","w",stdout);
51     while(scanf("%d%d",&n,&m)!=EOF) {
52         rep(i,1,n) scanf("%d",&val[i]),in[i]=0,d[i]=-inf,head[i]=0,C[i]=0;// bug C=0
53         num_e=0;
54         rep(i,1,m) {
55             int u=gi(),v=gi();
56             add(u,v);in[v]++;C[u]++;
57         }
58         topsort();
59         int Max=-inf;
60         rep(i,1,n)
61             if(!C[i])//  The val can be -
62                 Max=max(Max,d[i]);
63         printf("%d
",Max);
64     }
65     return 0;
66 }