【解题报告】poj3249_Test for Job(有向图记忆化搜索)
来源:互联网 发布:英雄联盟有mac版本吗 编辑:程序博客网 时间:2024/06/06 07:28
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
#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;}
- 【解题报告】poj3249_Test for Job(有向图记忆化搜索)
- poj3249Test for Job解题报告
- POJ 3249 Test for Job 解题报告 DP
- HDU 3249 Test for job (有向无环图上的最长路,DP)
- 有向图强连通分量(Kosaraju算法)+POJ 2762解题报告
- POJ 3249 Test for Job(记忆化搜索)
- USACO Job Processing 解题报告
- poj 1088 解题报告(记忆化搜索)
- POJ 1088 滑雪 解题报告(记忆化搜索)
- POJ3249 Test for Job(记忆化搜索)
- poj3249Test for Job记忆化搜索
- poj3249(记忆化搜索)Test for Job
- POJ1011 Sticks解题报告(经典搜索)
- POJ 3414 Pots 解题报告(搜索)
- aoj0558解题报告(广度优先搜索)
- NOIP2011 mayan游戏 解题报告(搜索)
- NOIP2015 斗地主 解题报告(搜索)
- TYVJ搜索解题报告
- 如何提高sql的性能
- weblogic上JDBC的配置
- java多线程
- weblogic上服务器建立
- Ueditor的两种定制方式
- 【解题报告】poj3249_Test for Job(有向图记忆化搜索)
- struts2工作原理
- 代码面试最常用的10大算法
- 实验3.2 复杂的单表查询
- java日期操作常用工具
- openssl升级到1.0.1g的过程
- iOS7中实现长按操作
- javaScript(1)---概述
- 我的java学习日记(9)