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. Hibernate save()、saveOrUpdate()、merge()的区别

    一. update 和 merge的区别 首先在执行更新操作的时候,两者都必须要有id update是直接执行update 语句,执行后状态为持久化状态 而merge则不一样: 1. 如果sessio ...

  2. 初始Mybatis,好累,自己感觉自己快坚持不了了

    Mybatis1.持久化 持久化,就是内存数据和硬盘数据状态的转换 2.ORM思想Object Relation Mapping 对象关系映射 3.MyBatis入门案例 3.1导入jar包 依赖 & ...

  3. Java面试:投行的15个多线程和并发面试题(转)

    多线程和并发问题已成为各种 Java 面试中必不可少的一部分.如果你准备参加投行的 Java 开发岗位面试,比如巴克莱银行(Barclays).花旗银行(Citibank).摩根史坦利投资公司(Mor ...

  4. Java编程基础-变量

    1.变量的定义. 变量与常量相对应,变量是在程序运行过程中它的值允许改变的量,变量可以通过变量名访问. 2.Java中的三大变量 (1).类变量.又称为静态变量,在类中定义类的属性时,使用static ...

  5. poj 2406 Power Strings 周期问题

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48139   Accepted: 20040 D ...

  6. 利用nodejs读取数据库数据生成树结构的json数据

    在做后台管理界面的时候,几乎少不了的一个结构就是树形结构,用来做菜单导航: 那么,最希望的就是树结构的所有数据都是读取的数据库,而不是直接代码当中写死,那我们就一步一步来看: 一,建表 字段通常包括: ...

  7. React Native 手工搭建环境 之iOS篇

    常识 React native 开发服务器 在开发时,我们的框架是这样的:  当正式发布进入到生产环境时,开发服务器上所有的js文件将会被编译成包的形式,直接嵌入到客户端内.这时,已经不再需要开发服 ...

  8. UVA10917 A walk trough the Forest (最短路,dp)

    求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]. 然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v]). #includ ...

  9. Heacher互助平台 α版本冲刺

    课程属性 作业课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 作业链接 https://edu.cnblogs.co ...

  10. Makefile入门教程

    Makefile介绍 make是一个命令工具,它解释Makefile 中的指令(应该说是规则).在Makefile文件中描述了整个工程所有文件的编译顺序.编译规则.Makefile 有自己的书写格式. ...