A. Test for Job
A. Test for Job
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 Viindicates 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 解题:记忆化搜索,找出值最大的路径。。。。返回该值。。。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int INF = ;
const int maxn = ;
vector<int>e[maxn];
int ret[maxn],in[maxn],w[maxn];
int n,m;
int dfs(int u){
if(ret[u] != -INF) return ret[u];
ret[u] = w[u];
int i,temp,ans = -INF;
for(i = ; i < e[u].size(); i++){
temp = dfs(e[u][i]);
if(temp > ans) ans = temp;
}
if(ans != -INF) ret[u] += ans;
return ret[u];
}
int main(){
int i,x,y,ans,temp;
while(~scanf("%d%d",&n,&m)){
for(i = ; i <= n; i++){
e[i].clear();
ret[i] = -INF;
scanf("%d",w+i);
}
memset(in,,sizeof(in));
for(i = ; i < m; i++){
scanf("%d %d",&x,&y);
e[x].push_back(y);
in[y]++;
}
ans = -INF;
for(i = ; i <= n; i++){
if(!in[i]){
temp = dfs(i);
if(temp > ans) ans = temp;
}
}
printf("%d\n",ans);
}
return ;
}
随机推荐
- 删除所有约束、表、视图等SQL脚本
--删除所有约束.表.视图等SQL脚本 --############################################### --删除所有外键约束 --################# ...
- 【5岁小孩都会】vs2013如何进行单元测试
1,如何进行单元测试呢,打开vs 新建一个项目 然后在解决方案右键点击,如下图所示: 2,左侧点击 测试 ->单元测试项目 3)点击确定,如下图 4)在当前代码上右键点击,调试 或者运行测试 ...
- 终于 Vue.js 成为世界一流的框架
终于 Vue.js 成为世界一流的框架 随着美团开源基于 Vue.js 的微信小程序框架 mpvue, Vue.js 在微信小程序端的能力被补齐,于是 Vue.js 成为了一个唯一能在 Web, H5 ...
- How to detect the presence of the Visual C++ 2010 redistributable package
Question: I have seen your previous blog posts that describe how to detect the presence of the Visua ...
- 国内的Jquery CDN免费服务
Jquery是个非常流行的JS前端框架,在很多网站都能看到它的身影.很多网站都喜欢采用一些Jquery CDN加速服务,这样网站加载jquery会更快.之前火端网络的一些网站都是使用Google的jq ...
- 基于 Azure IaaS 搭建企业官网的规划和实践
本课程主要介绍了基于 Azure IaaS 搭建企业官网的案例分析和实践,实践讲解如何使用 Azure 门户创建虚拟机, 创建虚拟网络, 创建存储账户等. 具体包括项目背景介绍, 项目架构, 准备和实 ...
- mongodb主从配置信息查看与确认
在local库中不仅有主从日志 oplog集合,还有一个集合用于记录主从配置信息 system.replset: > use local > show collections > d ...
- ActiveMQ消息丢失怎么解决?
在消息发送过程中消息丢失的话该怎么解决(包括网络原因): 解决思路: 可以把消息唯一ID,存到表里面,当消息接受端可以获取到这个ID,就给服务端一个回复IF,消息发送出去,没有回复,THEN一直循环发 ...
- root.sh脚本支持checkpoints文件实现重复运行
安装集群GRID/GI一般包括三个过程:首先,运行OUI/RunInstaller输入集群配置信息,其次,拷贝/编译集群文件,最后,以root用户运行root.sh脚本配置集群/启动集群,其中运行ro ...
- 1.JOIN和UNION区别
1.JOIN和UNION区别join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 . JOIN用于按照ON条件联接两 ...