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

  1. 6 5
  2. 1
  3. 2
  4. 2
  5. 3
  6. 3
  7. 4
  8. 1 2
  9. 1 3
  10. 2 4
  11. 3 4
  12. 5 6
 

Sample Output

  1. 7
  2.  
  3. 解题:记忆化搜索,找出值最大的路径。。。。返回该值。。。
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <climits>
  7. #include <algorithm>
  8. #include <cmath>
  9. #define LL long long
  10. using namespace std;
  11. const int INF = ;
  12. const int maxn = ;
  13. vector<int>e[maxn];
  14. int ret[maxn],in[maxn],w[maxn];
  15. int n,m;
  16. int dfs(int u){
  17. if(ret[u] != -INF) return ret[u];
  18. ret[u] = w[u];
  19. int i,temp,ans = -INF;
  20. for(i = ; i < e[u].size(); i++){
  21. temp = dfs(e[u][i]);
  22. if(temp > ans) ans = temp;
  23. }
  24. if(ans != -INF) ret[u] += ans;
  25. return ret[u];
  26. }
  27. int main(){
  28. int i,x,y,ans,temp;
  29. while(~scanf("%d%d",&n,&m)){
  30. for(i = ; i <= n; i++){
  31. e[i].clear();
  32. ret[i] = -INF;
  33. scanf("%d",w+i);
  34. }
  35. memset(in,,sizeof(in));
  36. for(i = ; i < m; i++){
  37. scanf("%d %d",&x,&y);
  38. e[x].push_back(y);
  39. in[y]++;
  40. }
  41. ans = -INF;
  42. for(i = ; i <= n; i++){
  43. if(!in[i]){
  44. temp = dfs(i);
  45. if(temp > ans) ans = temp;
  46. }
  47. }
  48. printf("%d\n",ans);
  49. }
  50. return ;
  51. }

随机推荐

  1. storm trident的filter和函数

    目的:通过kafka输出的信息进行过滤,添加指定的字段后,进行打印 SentenceSpout: package Trident; import java.util.HashMap; import j ...

  2. leetcode378 Kth Smallest Element in a Sorted Matrix

    思路1: 使用堆. 实现: class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...

  3. Android Studio 升级到3.0后出现编译错误\.gradle\caches\transforms-1\files-1.1\*****-release.aar

    Android Studio 升级到3.0后出现各种编译问题,其中有一个问题是关于资源找不到的问题,百度了半天,也没有相关的文章 C:\Users.gradle\caches\transforms-1 ...

  4. 洛谷 P2936 [USACO09JAN]全流Total Flow

    题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...

  5. Android(java)学习笔记139:Android中Menu的使用(静态 和 动态)

    1. 使用xml定义Menu(静态方法) 菜单资源文件必须放在res/menu目录中.菜单资源文件必须使用<menu>标签作为根节点.除了<menu>标签外,还有另外两个标签用 ...

  6. sparkmlib-相关系数

    一.基本原理 在stat包中实现了皮尔逊(Pearson)与 斯皮尔曼(Spearman)两类相关系数的计算 (1)Pearson:   (x,y)协方差/[(x标准方差)*(y标准方差)] 详情可以 ...

  7. 三、绘图和可视化之matplotlib

    #matplotlib简单绘图之plot import matplotlib.pyplot as plt a=[1,2,3] b=[10,2,30] plt.plot(a)#纵坐标为a的值,横坐标为a ...

  8. Sniper OJ部分writeup

    0x00 shellcode pwn 因为题目直接有源码,我就不拖进IDA了,直接看代码 这是一个典型的栈溢出,我们只需要构造shellcode获得/bin/sh权限就可以得到flag.下面是所用脚本 ...

  9. MYSQL 注射精华

    前言鄙人今天心血来潮突然想写篇文章,鄙人从来没写过文章,如果有错误的地方请多多指教.本文需要有基础的SQL语句知识才可以更好的理解.建议想学习的人多去了解一下SQL语句和编程语言,知己知彼才能百战百胜 ...

  10. shell脚本,实现奇数行等于偶数行。

    请把如下字符串stu494e222fstu495bedf3stu49692236stu49749b91转为如下形式:stu494=e222fstu495=bedf3stu496=92236stu497 ...