POJ3249 Test for Job 【DAG】+【记忆化搜索】

来源:互联网 发布:淡斑 知乎 编辑:程序博客网 时间:2024/04/28 02:33

Test for Job
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 9201 Accepted: 2080

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, 落叶飞雪
题意:求某一原点到终点的最大点权和。

DAG:4875ms(反向建图)  TLE(正向建图)

#include <stdio.h>#include <string.h>#define inf 0x7fffffff#define maxn 100002#define maxm 1000002int id, cost[maxn], sta[maxn];struct Node2{    int v, first;} head[maxn];struct Node{    int to, next;} E[maxm];bool vis[maxn], in[maxn], out[maxn];void addEdge(int u, int v){    E[id].to = v; E[id].next = head[u].first;    head[u].first = id++;}void getMap(int n, int m){    int i, u, v;    for(i = 1; i <= n; ++i){        scanf("%d", &head[i].v);        head[i].v = -head[i].v; in[i] = 0;        head[i].first = -1; out[i] = 0;        vis[i] = 0; cost[i] = inf;    }    for(i = id = 0; i < m; ++i){        scanf("%d%d", &u, &v); out[v] = 1;        addEdge(v, u); in[u] = 1;    }}void DAG(int n){    int i, u, v, id2 = 0, ans = inf, tmp;    for(i = 1; i <= n; ++i)        if(!in[i]){            sta[id2++] = i; vis[i] = 1;            cost[i] = head[i].v;            if(!out[i] && cost[i] < ans) ans = cost[i];        }    while(id2){        u = sta[--id2]; vis[u] = 0;        for(i = head[u].first; i != -1; i = E[i].next){            v = E[i].to; tmp = cost[u] + head[v].v;            if(tmp < cost[v]){                cost[v] = tmp;                if(!vis[v]){                    vis[v] = 1; sta[id2++] = v;                }                if(!out[v] && tmp < ans) ans = tmp;            }        }    }    //if(ans < 0) ans = - ans;    printf("%d\n", -ans);}int main(){    int n, m, i, u, v;    while(scanf("%d%d", &n, &m) == 2){        getMap(n, m);        DAG(n);    }    return 0;}

再放一个记忆化搜索:1875ms

#include <stdio.h>#include <string.h>#define inf 0x7fffffff#define maxn 100002#define maxm 1000002int dp[maxn], val[maxn], id;int head[maxn];struct Node{    int to, next;} E[maxm];bool in[maxn], out[maxn];void addEdge(int u, int v){    E[id].to = v;     E[id].next = head[u];    head[u] = id++;}void getMap(int n, int m){    int i, u, v;    for(i = 1, id = 0; i <= n; ++i){        scanf("%d", &val[i]);        in[i] = out[i] = 0; dp[i] = -inf;        head[i] = -1;    }    for(i = 0; i < m; ++i){        scanf("%d%d", &u, &v);        addEdge(u, v); in[v] = out[u] = 1;    }}int DFS(int k){    if(dp[k] != -inf) return dp[k];    int i, ans = -inf, tmp;    for(i = head[k]; i != -1; i = E[i].next){        tmp = DFS(E[i].to);        if(tmp > ans) ans = tmp;    }    dp[k] = ans + val[k];    return dp[k];}void solve(int n){    int i, ans = -inf, tmp;    for(i = 1; i <= n; ++i)        if(!out[i]) dp[i] = val[i];    for(i = 1; i <= n; ++i){        if(!in[i]){            tmp = DFS(i);            if(tmp > ans) ans = tmp;        }    }    printf("%d\n", ans);}int main(){    int n, m;    while(scanf("%d%d", &n, &m) == 2){        getMap(n, m);        solve(n);    }    return 0;}


0 0