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 ;
}
最新文章
- html中的图像动态加载问题
- hibernate(三)基本配置,log4j、JUnit配置
- Android:View中的performClick()触发条件
- Protostuff序列化
- 基本Socket通信流程
- linux笔记:linux常用命令-链接命令
- 初识 istringstream、ostringstream、stringstream 运用
- elasticsearch的5种分片查询优先级
- 可扩展标记语言XML
- [2017BUAA软工助教]收集个人信息
- ch11 持有对象
- JavaScript中对象数组 作业
- 插值代码17个---MATLAB
- 如何启用小米手机5c的ROOT权限
- redis学习笔记01 — 基本介绍、安装配置及常用命令
- 007-Python函数-装饰器
- POJ 2029 Get Many Persimmon Trees (模板题)【二维树状数组】
- 【转】VS2015详细安装步骤
- python一个简单的websocket测试客户端
- 关于Django的Ajax操作