题干:

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 Viof 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

解题报告:

  因为有负权值所以初值要注意初始值要是-INF,并且只能用vis数组来标记是否走过,不能用dp[v]=-1来判断是否走过,因为这里的dp有负权值所以不能把-1当成非法状态。

  这题也可以拓扑排序,每次弹出入度为0的顶点来更新其他节点,不断维护e[i].v的最大值(因为e[i].u这个值是完成值也就是保证了是最优解,所以只能用我为人人法去更新),这样也可以做到On的复杂度。

其实两种方法一个是递推dp(按照拓扑序当dp的阶段)一个是记忆化搜索,本质是一样的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e6 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {
	int to,ne;
} e[MAX];
int head[MAX],in[MAX],out[MAX],tot,val[MAX],n,m;
void add(int u,int v) {
	e[++tot].to = v;;
	e[tot].ne = head[u];
	head[u] = tot;
}
int dp[MAX],vis[MAX];
int dfs(int cur,int rt) {
	if(vis[cur] == 1) return dp[cur];
	vis[cur] = 1;
	int res = -INF;//int res = val[cur];这样写的话应该就错了不信一会你自己试试 
	for(int i = head[cur]; ~i; i = e[i].ne) {
		int v = e[i].to;
		res = max(res,dfs(v,cur));
	}
	if(head[cur] == -1) return dp[cur] = val[cur];
	else return dp[cur] = res + val[cur];
}
int main()
{
	while(~scanf("%d%d",&n,&m)) {
		tot=0;
		for(int i = 1; i<=n; i++) head[i] = -1,in[i]=out[i]=vis[i]=0,dp[i] = 0;
		for(int i = 1; i<=n; i++) scanf("%d",val+i);
		for(int u,v,i = 1; i<=m; i++) {
			scanf("%d%d",&u,&v),add(u,v);
			in[v]++;out[u]++;
		}
		int ans = -INF;//如果用0来赋初值的话呢?会有什么后果》 
		for(int i = 1; i<=n; i++) {
			if(in[i] == 0) {
				ans = max(ans,dfs(i,-1));
			}
		}
		printf("%d\n",ans);
	} 
	return 0 ;
}