【解题报告】poj3249_Test for Job(有向图记忆化搜索)

来源:互联网 发布:英雄联盟有mac版本吗 编辑:程序博客网 时间:2024/06/06 07:28
Test for Job
Time Limit: 5000MS Memory Limit: 65536KTotal 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 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 51223341 21 32 43 45 6

Sample Output

7

Hint

Source

POJ Monthly--2007.07.08, 落叶飞雪


题目大意:
给定有向无环图,每个点有权值,权值可为负,求以任意一入度为0的点为起点,到达任意一出度为0的点的最长路径(所经过点的权值和)为多少。

#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;}



0 0