【POJ - 3249】Test for Job(DAG线性求带负权的最长路,dp)

题干:

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 Viof all cities he may reach (a negative Vi indicates 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

Hint

解题报告:

  因为有负权值所以初值要注意初始值要是-INF,并且只能用vis数组来标记是否走过,不能用dp[v]=-1来判断是否走过,因为这里的dp有负权值所以不能把-1当成非法状态。

  这题也可以拓扑排序,每次弹出入度为0的顶点来更新其他节点,不断维护e[i].v的最大值(因为e[i].u这个值是完成值也就是保证了是最优解,所以只能用我为人人法去更新),这样也可以做到On的复杂度。

其实两种方法一个是递推dp(按照拓扑序当dp的阶段)一个是记忆化搜索,本质是一样的。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define F first
#define S second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 2e6 + 5;
const int INF = 0x3f3f3f3f;
struct Edge {int to,ne;
} e[MAX];
int head[MAX],in[MAX],out[MAX],tot,val[MAX],n,m;
void add(int u,int v) {e[++tot].to = v;;e[tot].ne = head[u];head[u] = tot;
}
int dp[MAX],vis[MAX];
int dfs(int cur,int rt) {if(vis[cur] == 1) return dp[cur];vis[cur] = 1;int res = -INF;//int res = val[cur];这样写的话应该就错了不信一会你自己试试 for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to;res = max(res,dfs(v,cur));}if(head[cur] == -1) return dp[cur] = val[cur];else return dp[cur] = res + val[cur];
}
int main()
{while(~scanf("%d%d",&n,&m)) {tot=0;for(int i = 1; i<=n; i++) head[i] = -1,in[i]=out[i]=vis[i]=0,dp[i] = 0;for(int i = 1; i<=n; i++) scanf("%d",val+i);for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v),add(u,v);in[v]++;out[u]++;}int ans = -INF;//如果用0来赋初值的话呢?会有什么后果》 for(int i = 1; i<=n; i++) {if(in[i] == 0) {ans = max(ans,dfs(i,-1));}}printf("%d\n",ans);} return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/440186.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

图解算法学习笔记(二): 选择排序

目录 1)数组和链表&#xff1a; 2)选择排序算法&#xff1a; 3)小结 本章内容&#xff1a; 两种基本数据结构&#xff1a;数组和链表&#xff1b; 选择排序算法&#xff1b; 1)数组和链表&#xff1a; 数组是连续的内存单元&#xff0c;链表可以不连续&#xff1b; 链表…

javascript递归遍历文件夹下面的所有文件并返回所有文件全路径名称数组以及解析JavaScript方法体字符串的结束位置

一、前端脚本经常需要用到遍历指定文件夹下面的所有文件&#xff08;包含子文件夹&#xff09;的内容并做特定的逻辑处理&#xff0c;下面给出同步遍历的方式&#xff0c;开箱即用。 const fs require(fs);main()function main() {let allFiles getAllFiles(srcDir);console…

WinXP下替代IIS的新思路

WinXP下&#xff0c;.Net服务器有两个选择&#xff0c;IIS和Webdev.webservice。然而&#xff0c;IIS有最多十连接的限制&#xff0c;网上的解决方式&#xff08;包括注册表修改、微软工具、NTSwitch&#xff09;均未能突破&#xff1b;webdev.webservice虽没有连接数量限制&am…

【HDU - 5456】Matches Puzzle Game(数位dp,思维)

题干&#xff1a; As an exciting puzzle game for kids and girlfriends, the Matches Puzzle Game asks the player to find the number of possible equations A−BCA−BC with exactly n (5≤n≤500)n (5≤n≤500) matches (or sticks). In these equations, A,BA,B and …

图解算法学习笔记(三):递归

本章内容&#xff1a; 学习递归&#xff1b;如何将问题分解成基线条件和递归条件。 1) 每个递归函数都有两部分&#xff1a;基线条件(base case)和递归条件(recursive base)。例如&#xff1a;打印3...2...1 def countdown(i):print(i)if i < 0:returnelse:countdown(i…

深入理解Angular模块化概念

深入理解Angular模块NgModule装饰器 Angular应用程序全部是由 模块化组成的 &#xff0c;即模块化开发&#xff08;组件/指令/服务/管道/路由&#xff09;&#xff0c;与js模块化是不同的概念&#xff0c;但具有异曲同工之妙&#xff1b; Angular 模块之间是隔离 的&#xff0…

在IIS中启用父路径,不被黑客利用

在IIS中&#xff0c;有时要启用父路径&#xff0c;但黑客常常利用父路径访问硬盘文件。因此&#xff0c;我使用了一种方法&#xff1a; 先将IIS暂停&#xff0c;启用父路径。之所以暂停是为了防止操作时遭攻击然后&#xff0c;在根目录下创建虚拟路径“..”&#xff0c;任意选择…

【HRBUST - 1996】数学等式 (HASH 或 二分)

题干&#xff1a; 又到了数学题的时刻了&#xff0c;给出三个数组A,B,C,然后再给出一个数X&#xff0c;现在我想知道是否能找到三个数满足等式A[i]B[j]C[k]X&#xff0c;你能帮助我么&#xff1f;&#xff1f; Input 本题有多组数据&#xff0c;每组数据第一行输入三个数n, …

Apollo自动驾驶入门课程第⑨讲 — 控制(上)

目录 1. 简介 2. 控制流程 3. PID控制 4. PID优劣对比 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 9月26日 上周我们发布了无人驾驶技术的 规划篇&#xff0c;车辆基于高精地图&#xff0c;感知和预测模块的数据来进行这一…

Angular高版本中为自定义的独立class类添加显式的Angular装饰器

Angular9或10及以后的版本&#xff0c;如果自定义的类上面没有写装饰器的话&#xff0c;编译后在Browser平台不会报错&#xff0c;但是在执行打包命令npm run build --prod时就会报错如下所示&#xff1a; error NG2007: Class is using Angular features but is not decorate…

Python操作Kafka爬坑

组内做大数据&#xff0c;需要kafka写入数据&#xff0c;最近在看python正好&#xff0c;练练手&#xff0c;网上找了一圈&#xff0c;都是用的pykafka&#xff0c;经过一整圈的安装&#xff0c;最终搞定&#xff0c;代码如下#coding:u8import sysimport timeimport randomimpo…

Apollo自动驾驶入门课程第⑩讲 — 控制(下)

目录 1. 线性二次调节器 2. 模型控制预测 3. 总结 本文转自微信公众号&#xff1a;Apollo开发者社区 原创&#xff1a; 阿波君 Apollo开发者社区 昨天 Apollo自动驾驶课程马上进入尾声&#xff0c;在无人驾驶技术控制篇&#xff08;上&#xff09;中&#xff0c;具体讲解了最…

*【ZOJ - 3703】Happy Programming Contest(带优先级的01背包)

题干&#xff1a; In Zhejiang University Programming Contest, a team is called "couple team" if it consists of only two students loving each other. In the contest, the team will get a lovely balloon with unique color for each problem they solved.…

图解算法学习笔记(四):快速排序

目录 1&#xff09; 示例1&#xff1a; 2&#xff09;快速排序 3) 再谈大O表示法 4&#xff09;小结 本章内容&#xff1a;学习分而治之&#xff0c;快速排序 1&#xff09; 示例1&#xff1a; 假设你是农场主&#xff0c;有一小块土地&#xff0c;你要将这块地均匀分成方…

ThriftParserError: ThriftPy does not support generating module with path in protocol 'd'

使用python连接hive&#xff0c;在 from impala.dbapi import connect 语句报如下错误&#xff1a; ThriftParserError: ThriftPy does not support generating module with path in protocol d 定位到 D:\Anaconda3\Lib\site-packages\thriftpy\parser\parser.py的 if u…

【HDU - 5468】Puzzled Elena(容斥原理,dfs序,数学,素因子分解,有坑)

题干&#xff1a; Problem Description Since both Stefan and Damon fell in love with Elena, and it was really difficult for her to choose. Bonnie, her best friend, suggested her to throw a question to them, and she would choose the one who can solve it.Suppo…

图解算法学习笔记(五):散列表

目录 1&#xff09;示例1&#xff1a; 2&#xff09;散列函数 3&#xff09;应用案例 4&#xff09;冲突 5&#xff09;性能 6&#xff09;小结 本章内容&#xff1a; 学习散列表&#xff0c;最有用的数据结构之一。 学习散列表的内部机制&#xff1a;实现、冲突和散列函…

Ros 消息结构1

1、ROS的消息头信息 #Standard metadata for higher-level flow data types #sequence ID: consecutively increasing ID uint32 seq#Two-integer timestamp that is expressed as: # * stamp.secs: seconds (stamp_secs) since epoch # * stamp.nsecs: nanoseconds since sta…

【HDU - 5475】An easy problem(线段树,思维)

题干&#xff1a; One day, a useless calculator was being built by Kuros. Lets assume that number X is showed on the screen of calculator. At first, X 1. This calculator only supports two types of operation. 1. multiply X with a number. 2. divide X with…

图解算法学习笔记(六):广度优先搜索

目录 1&#xff09;图简介 2&#xff09;图是什么 3&#xff09;广度优先搜索 4&#xff09;实现图 5&#xff09;实现算法 6&#xff09;小结 本章内容; 学习使用新的数据结构图来建立网络模型&#xff1b; 学习广度优先搜索&#xff1b; 学习有向图和无向图…