POJ 3249 Test for Job(拓扑排序+dp优化空间)
Description
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 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 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 i, Vi (0 ≤ |Vi| ≤ 20000)
The next m lines each contain two integers x, y 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
Sample Input
6 5
1
2
2
3
3
4
1 2
1 3
2 4
3 4
5 6
Sample Output
7
Hint

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
#include "string"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + ;
int n, m, num;
int cost[MAXN], in[MAXN], out[MAXN], head[MAXN], dp[MAXN];
bool vis[MAXN];
struct node
{
/* data */
int fr, to, nxt;
}e[MAXN * ];
void add(int x, int y)
{
e[num].fr = x;
e[num].to = y;
e[num].nxt = head[x];
head[x] = num++;
}
void toposort()
{
int cnt = ;
while(cnt < n) {
for(int i = ; i <= n; ++i)
if(in[i] == && !vis[i]) {
vis[i] = true;
cnt++;
for(int j = head[i]; j != -; j = e[j].nxt) {
int x = e[j].to;
in[x]--;
if(dp[i] + cost[x] > dp[x]) dp[x] = dp[i] + cost[x];
}
}
}
}
int main(int argc, char const *argv[])
{
while(scanf("%d%d", &n, &m) != EOF) {
memset(in, , sizeof(in));
memset(out, , sizeof(out));
memset(head, -, sizeof(head));
memset(vis, false, sizeof(vis));
num = ;
for(int i = ; i <= n; ++i)
scanf("%d", &cost[i]);
for(int i = ; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
in[y]++;
out[x]++;
}
for(int i = ; i <= n; ++i)
if(in[i] == ) dp[i] = cost[i];
else dp[i] = -INF;
toposort();
int ans = -INF;
for(int i = ; i <= n; ++i)
if(out[i] == && dp[i] > ans) ans = dp[i];
printf("%d\n", ans);
}
return ;
}
代码转自(https://blog.csdn.net/gkhack/article/details/50223357)
POJ 3249 Test for Job(拓扑排序+dp优化空间)的更多相关文章
- POJ 3249 Test for Job (拓扑排序+DP)
POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- [NOIP2017]逛公园 最短路+拓扑排序+dp
题目描述 给出一张 $n$ 个点 $m$ 条边的有向图,边权为非负整数.求满足路径长度小于等于 $1$ 到 $n$ 最短路 $+k$ 的 $1$ 到 $n$ 的路径条数模 $p$ ,如果有无数条则输出 ...
- 洛谷P3244 落忆枫音 [HNOI2015] 拓扑排序+dp
正解:拓扑排序+dp 解题报告: 传送门 我好暴躁昂,,,怎么感觉HNOI每年总有那么几道题题面巨长啊,,,语文不好真是太心痛辣QAQ 所以还是要简述一下题意,,,就是说,本来是有一个DAG,然后后来 ...
- 【BZOJ-1194】潘多拉的盒子 拓扑排序 + DP
1194: [HNOI2006]潘多拉的盒子 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 456 Solved: 215[Submit][Stat ...
- 【BZOJ5109】[CodePlus 2017]大吉大利,晚上吃鸡! 最短路+拓扑排序+DP
[BZOJ5109][CodePlus 2017]大吉大利,晚上吃鸡! Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏 ...
- bzoj1093[ZJOI2007]最大半连通子图(tarjan+拓扑排序+dp)
Description 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:?u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u ...
- 【bzoj4011】[HNOI2015]落忆枫音 容斥原理+拓扑排序+dp
题目描述 给你一张 $n$ 个点 $m$ 条边的DAG,$1$ 号节点没有入边.再向这个DAG中加入边 $x\to y$ ,求形成的新图中以 $1$ 为根的外向树形图数目模 $10^9+7$ . 输入 ...
- 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp
题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:对于u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. ...
随机推荐
- 吴裕雄--天生自然ShellX学习笔记:Shell 变量
定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一 ...
- UML-FURPS+与补充性规格说明
1.FURPS+ 在统一过程(UP)中,需求按照“FURPS+”模型进行分类. 功能性(Functional):特性.功能.安全性: 可用性(Usability):人性化因素.帮助.文档: 可靠性(R ...
- day65-CSS选择器和样式优先级
1. CSS CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). 2.CSS语法 每个 ...
- ZJNU 1365 - Window--中级
每次都寻找长度为k的区间内的最小值显然很容易超出时间限制 所以可以把窗户看作一个数量固定的队列 每次观察入列与出列的元素对答案贡献如何,以更新答案 /* Written By StelaYuri */ ...
- mysql 获取数据库和表结构信息
SELECT * FROM information_schema.`TABLES` where TABLE_SCHEMA = '数据库名';SELECT * FROM information_sche ...
- aop 实现原理
aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...
- Angular(三)
Angular开发者指南(三)数据绑定 数据绑定AngularJS应用程序中的数据绑定是模型和视图组件之间的数据的自动同步. AngularJS实现数据绑定的方式可以将模型视为应用程序中的单一来源 ...
- 吴裕雄--天生自然 PYTHON3开发学习:面向对象
class MyClass: """一个简单的类实例""" i = 12345 def f(self): return 'hello wor ...
- Linux之seq命令
作用:seq命令用于以指定增量从首数开始打印数字到尾数,即产生从某个数到另外一个数之间的所有整数,并且可以对整数的格式.宽度.分割符号进行控制 语法: [1] seq [选项] 尾数 [2] ...
- JavaWeb过滤器(Filter)
参考:https://blog.csdn.net/yuzhiqiang_1993/article/details/81288912 原理: 一般实现流程: 1.新建一个类,实现Filter接口2.实现 ...