首页 > 代码库 > POJ3249 Test for Job 【DAG】+【记忆化搜索】
POJ3249 Test for Job 【DAG】+【记忆化搜索】
Time Limit: 5000MS | Memory Limit: 65536K | |
Total 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 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
DAG:
#include <stdio.h> #include <string.h> #define inf 0x7fffffff #define maxn 100002 #define maxm 1000002 int 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; }再放一个RE的记忆化搜索。。。:
#include <stdio.h> #include <string.h> #define inf 0x7fffffff #define maxn 100002 #define maxm 1000002 int dp[maxn], id; struct Node2{ int first, v; } 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].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] = out[i] = 0; dp[i] = inf; head[i].first = -1; } for(i = 0; i < m; ++i){ scanf("%d%d", &u, &v); addEdge(u, v); in[v] = 1; out[u] = 1; } } int DFS(int k) { if(dp[k] != inf) return dp[k]; int i, u, v, ans = inf, tmp; for(i = head[k].first; i != -1; i = E[i].next){ tmp = head[k].v + DFS(E[i].to); if(tmp < ans) ans = tmp; } return dp[k] = ans; } void solve(int n) { int i, u, v, ans = inf, tmp; for(i = 1; i <= n; ++i) if(!out[i]) dp[i] = head[i].v; 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; }