Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 8708 | Accepted: 1940 |
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
Source
#include <cstdio>
#include <cstring>
#define max(a,b) (a>b?a:b)
const int MAX_N = 100010;
const int MAX_M = 1000010;
const long long INF = 20000000000LL;
int n;//顶点数
int m;//边数
int profit[MAX_N];//每点利润(可为负)
int head[MAX_N];//每点边集头指针
int indegree[MAX_N];//每点入度
long long dp[MAX_N];//以每点为起点的最长路解
struct {
int to;//目的节点
int w;//权值
int next;//next边
}edge[MAX_M];//边集
long long dfs(int t) {//记忆化搜索
if(dp[t]!=-1) return dp[t];
long long max_r = -INF;
for(int i=head[t];i!=-1;i=edge[i].next) {
long long r = dfs(edge[i].to);
max_r = max(max_r,r);
}
dp[t] = profit[t];
if(max_r!=-INF) dp[t] += max_r;
return dp[t];
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d",&n,&m)) {
memset(head,-1,sizeof(head));
memset(indegree,0,sizeof(indegree));
memset(dp,-1,sizeof(dp));
for(int i=1;i<=n;++i) scanf("%d",profit+i);
for(int i=0,from,to;i<m;++i) {
scanf("%d%d",&from,&to);
edge[i].to = to;
edge[i].w = profit[to];
edge[i].next = head[from];
head[from] = i;
indegree[to] += 1;//入度+1
}
//for(int i=1;i<=n;++i) printf("%d ",head[i]);printf("\n");
//for(int i=0;i<m;++i) printf("i=%d to=%d w=%d next=%d\n",i,edge[i].to,edge[i].w,edge[i].next);
//for(int i=1;i<=n;++i) printf("%d ",indegree[i]);printf("\n");
long long res = -INF;
for(int i=1;i<=n;++i) if(!indegree[i]) {//以入度为0的点为起点开始搜索
long long r = dfs(i);
res = max(res,r);
}
printf("%lld\n",res);
}
return 0;
}