POJ 3249 Test for Job (记忆化搜索)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 11830 | Accepted: 2814 |
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
思路:
本来想用top排序后,用类似树上dp的方式来实现,但是想了一下,还要写top排序,感觉好麻烦,干脆直接搜索算了。当然要记忆化一下,直接跑了2700ms。。。
注意ans要初始化为负无穷。
代码:
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
int a[100086],dp[100086];
int in[100086],out[100086];
int n,m;
vector<int>u[100086]; int dfs(int t)
{
int siz=u[t].size();
if(dp[t]){return dp[t];}
if(out[t]==0){return dp[t]=a[t];}
int ans=-999999999;
for(int i=0;i<siz;i++){
ans=max(ans,dfs(u[t][i])+a[t]);
}
return dp[t]=ans;
} bool init()
{
if(scanf("%d%d",&n,&m)==EOF){
return false;
}
for(int i=1;i<=n;i++){
u[i].clear();
scanf("%d",&a[i]);
}
memset(dp,0,sizeof(dp));
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
u[x].push_back(y);
out[x]++;
in[y]++;
}
return true;
} int solve()
{
int ans=-999999999;
for(int i=1;i<=n;i++){
if(in[i]==0){
ans=max(dfs(i),ans);
}
}
return ans;
} int main()
{
while(init()){
printf("%d\n",solve());
}
}
POJ 3249 Test for Job (记忆化搜索)的更多相关文章
- poj 3249(bfs+dp或者记忆化搜索)
题目链接:http://poj.org/problem?id=3249 思路:dp[i]表示到点i的最大收益,初始化为-inf,然后从入度为0点开始bfs就可以了,一开始一直TLE,然后优化了好久才4 ...
- poj 1661 Help Jimmy(记忆化搜索)
题目链接:http://poj.org/problem?id=1661 一道还可以的记忆化搜索题,主要是要想到如何设dp,记忆化搜索是避免递归过程中的重复求值,所以要得到dp必须知道如何递归 由于这是 ...
- poj 1085 Triangle War 博弈论+记忆化搜索
思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...
- poj 1088 动态规划+dfs(记忆化搜索)
滑雪 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Description Mi ...
- poj 1579(动态规划初探之记忆化搜索)
Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17843 Accepted: 9112 ...
- POJ 3616 Milking Time ——(记忆化搜索)
第一眼看是线段交集问题,感觉不会= =.然后发现n是1000,那好像可以n^2建图再做.一想到这里,突然醒悟,直接记忆化搜索就好了啊..太蠢了.. 代码如下: #include <stdio.h ...
- POJ 1661 Help Jimmy ——(记忆化搜索)
典型的记忆化搜索问题,dfs一遍即可.但是不知道WA在哪里了= =,一直都没找出错误.因为思路是很简单的,肯定是哪里写挫了,因此不再继续追究了. WA的代码如下,希望日后有一天能找出错误= =: —— ...
- poj 3249 Test for Job (记忆化深搜)
http://poj.org/problem?id=3249 Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissi ...
- poj 1191 棋盘分割(dp + 记忆化搜索)
题目:http://poj.org/problem?id=1191 黑书116页的例题 将方差公式化简之后就是 每一块和的平方 相加/n , 减去平均值的平方. 可以看出来 方差只与 每一块的和的平方 ...
- poj 1579 Function Run Fun(记忆化搜索+dp)
题目链接:http://poj.org/problem?id=1579 思路分析:题目给出递归公式,使用动态规划的记忆搜索即可解决. 代码如下: #include <stdio.h> #i ...
随机推荐
- 用dbExpress页的SQLConnection1连接sql server2000怎么设置。 [问题点数:0分]
在d7或者c6已经支持了. 贡献一下我的代码吧:dbeConn:= TSQLConnection.Create(nil); dbeConn.Params.Clear; dbeC ...
- tensorflow点滴笔记
1.模型保存 模型保存需要使用函数 tf.train.Saver(), a)创建saver时,可以指定需要存储的tensor,如果没有指定,则全部保存. b) 创建saver时,可以指定保存的模型个数 ...
- 【python练习题】程序15
#题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. n = input('请输入成绩 :') n = int(n) if ...
- Linux成为云计算平台的主流操作系统
导读 这是一个人人谈"云"."大数据"的时代,作为一个IT民工,如果与同行间聊天时,不谈及这方面的内容,有人可能会觉得你落伍了,跟不上这个时代了. 这是一个人人 ...
- SPOJ705-New Distinct Substrings-后缀数组
计算所都不相同子串的个数,做法是所有子串的个数减去sigma(height[]).其中height数组的和便是所有相同子串的个数. 注意 N×(N+1)/2会爆int!但是最终答案在int内.所以使用 ...
- Phone List HDU - 1671 字典树
题意:给出一堆一组一组的数字 判断有没有哪一个是另外一个的前缀 思路:字典树 插入的同时进行判断 不过 当处理一组数字的时候 需要考虑的有两点1.是否包含了其他的序列2.是否被其他序列包含 刚开始 ...
- BZOJ4653 [NOI2016] 区间 【线段树】
题目分析: 首先思考一个二分答案的做法.我们可以注意到答案具有单调性,所以可以二分答案. 假设当前二分的答案是$ k $.那么按照大小顺序插入每个区间,同时在末端删除会对答案产生影响的区间.这里不妨用 ...
- 洛谷P1038神经网络题解
题目 这个题不得不说是一道大坑题,为什么这么说呢,这题目不仅难懂,还非常适合那种被生物奥赛刷下来而来到信息奥赛的学生. 因此我们先分析一下题目的坑点. 1: 题目的图分为输入层,输出层,以及中间层. ...
- Matplotlib学习---用seaborn画直方图,核密度图(histogram, kdeplot)
由于直方图受组距(bin size)影响很大,设置不同的组距可能会产生完全不同的可视化结果.因此我们可以用密度平滑估计来更好地反映数据的真实特征.具体可参见这篇文章:https://blog.csdn ...
- 慢腾腾的Quartus prime16.0加快编译速度
前言 当一个工程反复修改的时候,可能有时候源代码没有更改,为了加快编译速度可以配置quartus一些选项.当然,初次编译的速度是否会提升,未验证.更高级的设计分区以及逻辑锁区提升速度,以后阐述. 流程 ...