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. 使用aptana执行jruby

    Apatana Studio只会找ruby/bin的ruby执行档....为了在Apatana Studio用JRuby,除了设定好Path之外还要在JRuby/bin下建立一的ruby.bat,里面 ...

  2. 搭建高可用mongodb集群(一)——mongodb配置主从模式

    转载自:LANCEYAN.COM 在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. NOSQ ...

  3. Permutations(copy)

    Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...

  4. jmeter中通过jdbc方式连接mysql数据库的配置参考

    jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&allowMu ...

  5. sql server Cannot resolve the collation conflict between "Chinese_PRC_BIN" and "Chinese_PRC_CI_AS" in the equal to operation

    今天查询二个db,出现这个错误,二种方法,一种是把db里的collation改成一样的:如果不方便可以直接在sql语句后面转一下: select * from table where crm_mscr ...

  6. (十三)maven之release和snapshots

    发布release 用户A将代码打包到RELEASE仓库.用户B使用时,需要在pom.xml添加jar包的依赖坐标.如果用户A将jar包版本从1.0升级到2.0,用户B使用时也需要在pom.xml中修 ...

  7. 微信程序开发系列教程(三)使用微信API给微信用户发文本消息

    这个系列的第二篇教程,介绍的实际是被动方式给微信用户发文本消息,即微信用户关注您的公众号时,微信平台将这个关注事件通过一个HTTP post发送到您的微信消息服务器上.您对这个post请求做了应答(格 ...

  8. urllib基础-利用网站结构爬取网页-百度搜索

    有的时候爬取网页,可以利用网站额结构特点爬取网页 在百度搜索框中输入搜索内容,单击搜索,浏览器会发送一个带有参数的url请求.尝试删除其中的一些参数,只剩下wd这个参数.发现wd是搜索内容.这样程序可 ...

  9. VC-基础:VC中得到当前系统的时间和日期

    得到当前时间的方法一般都是得到从1900年0点0分到现在的秒数,然后转为年月日时分秒的形式得到当前的时间(时分秒).主要方法如下: 1)使用CRT函数 C++代码   ]; time_t nowtim ...

  10. Bootstrap 静态控件

    当您需要在一个水平表单内表单标签后放置纯文本时,请在 <p> 上使用 class .form-control-static. 实例: <!DOCTYPE html><ht ...