A. Test for Job

Time Limit: 5000ms
Case Time Limit: 5000ms
Memory Limit: 65536KB
 
64-bit integer IO format: %lld      Java class name: Main
 

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 input file includes several test cases.
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 iVi (0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integers xy 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

The output file contains one line for each test cases, in which contains an integer indicating the maximum profit Dog is able to obtain (or the minimum expenditure to spend)

 

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 ;
}

最新文章

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

热门文章

  1. P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
  2. JS排序--快速排序
  3. 简单的UDP程序
  4. 解决部分浏览器不能显示itext生成的PDF文件文本域内容问题
  5. IO多路复用机制(转)
  6. Java 图形界面开发--图文并茂建立学生管理系统
  7. 使用ABAP编程实现对微软Office Word文档的操作
  8. codeforce Gym 100203I I WIN (网络流)
  9. 使用memcached缓存 替代solr中的LRUCache缓存
  10. @ConditionalOnProperty来控制Configuration是否生效