Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 10209 | Accepted: 2372 |
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
思路:拓扑排序,DP。
因为图是有向的无环图,我们要找的是一个起点和一个中点,也就是一个入度为0,一个出度为0的之间的最大价值,每一个点我们可以用DP来
记录能走到这个点的最大价值,注意价值可能,<0,所以DP开始时要初始化最小,然后按照拓扑序去更新dp,最后找那些出度为0的点的最大值即可。
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<string.h> 5 #include<vector> 6 #include<queue> 7 using namespace std; 8 typedef long long LL; 9 LL co[100005]; 10 LL MM[100005]; 11 vector<int>vec[100005]; 12 int ans[100005]; 13 queue<int>que; 14 int main(void) { 15 int i,j,k,p,q; 16 int x,y; 17 while(scanf("%d %d",&p,&q)!=EOF) { 18 memset(ans,0,sizeof(ans)); 19 fill(MM,MM+100005,-100000000000); 20 for(i=0; i<100005; i++) 21 vec[i].clear(); 22 while(!que.empty()) 23 que.pop(); 24 for(i=1; i<=p; i++) { 25 scanf("%lld",&co[i]); 26 } 27 while(q--) { 28 scanf("%d %d",&x,&y); 29 vec[y].push_back(x); 30 ans[x]++; 31 } 32 LL maxx=-100000000000; 33 for(i=1; i<=p; i++) { 34 if(ans[i]==0) { 35 que.push(i); 36 MM[i]=co[i]; 37 } 38 } 39 while(!que.empty()) { 40 int c=que.front(); 41 que.pop(); 42 if(maxx<MM[c]&&vec[c].size()==0) 43 maxx=MM[c]; 44 for(i=0; i<vec[c].size(); i++) { 45 int cc=vec[c][i]; 46 ans[cc]--; 47 if(MM[cc]<MM[c]+co[cc]) 48 MM[cc]=MM[c]+co[cc]; 49 if(ans[cc]==0) 50 que.push(cc); 51 } 52 } 53 printf("%lld ",maxx); 54 } 55 return 0; 56 }