POJ3249 Test for Job 【DAG】+【记忆化搜索】
来源:互联网 发布:淡斑 知乎 编辑:程序博客网 时间:2024/04/28 02:33
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 51223341 21 32 43 45 6
Sample Output
7
Hint
Source
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;}
- POJ3249 Test for Job 【DAG】+【记忆化搜索】
- POJ3249 Test for Job(记忆化搜索)
- poj3249(记忆化搜索)Test for Job
- poj3249 Test for job 【图的DAG dp】
- [poj3249]Test For Job
- POJ3249 Test for Job
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
- POJ 3249 Test for Job(DAG上的dp + 记忆化搜索)
- poj3249 Test for Job --- 拓扑排序
- Test for Job(poj3249 拓扑+动态规划)
- POJ3249 Test for Job(拓扑排序+dp)
- POJ 3249 Test for Job(记忆化搜索)
- POJ 3249 Test for Job (记忆化搜索 好题)
- POJ -- 3249 Test for Job (记忆化搜索)
- Test for Job (poj 3249 记忆化搜索)
- poj 3249 Test for Job 图上dp(记忆化搜索)
- poj3249Test for Job记忆化搜索
- POJ - 3249 Test for Job (DAG+topsort)
- 本地js页面
- JAXB基本类型对应关系
- HDU 3549 Flow Problem
- hdoj 1022 Train Problem I(用栈解决法)
- Linux mint 下安装Fcitx中文输入法
- POJ3249 Test for Job 【DAG】+【记忆化搜索】
- 图片加水印工具类
- hdu3549 Flow Problem
- 秒杀多线程第十篇 生产者消费者问题
- 用WebCollector做Web挖掘(实例1)
- google 2015最新的校招测试题与思路
- TCP协议状态图
- ant debug最常见的一个错误解决
- 【备忘录】僵尸进程与孤儿进程以及一些进程间的知识