Test for Job 图上的动态规划(DAG)
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 11399 | Accepted: 2697 |
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

Source
#include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>
#include <stack>
#include <cstring>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
#define MAXN 200000+9
#define MAXM 2000000 + 9 struct edge
{
LL next, v, cost;
}E[MAXM];
LL n, m;
LL head[MAXN], tot;
LL cnt[MAXN], topsort[MAXN];
LL val[MAXN], dist[MAXN],out[MAXN];
void init()
{
tot = ;
memset(head, -, sizeof(head));
memset(cnt, , sizeof(cnt));
memset(out, , sizeof(out));
}
void addedge(LL f, LL t, LL d)
{
E[tot].v = t;
E[tot].cost = d;
E[tot].next = head[f];
head[f] = tot++;
}
LL Topsort()
{
LL p = ;
for (LL i = ; i <= n; i++)
{
if (cnt[i] == )
dist[i] = val[i], topsort[p++] = i, cnt[i]--;
else
dist[i] = -INF;
}
for (LL i = ; i < p; i++)
{
for (LL j = head[topsort[i]]; j != -; j = E[j].next)
{
LL v = E[j].v;
dist[v] = max(dist[v], dist[topsort[i]] + E[j].cost);
if (--cnt[v] == )
topsort[p++] = v;
}
}
LL ans = -INF;
for (int i = ; i <= n; i++)
if (!out[i])
ans = max(ans, dist[i]);
return ans; } int main()
{
while (scanf("%lld%lld", &n, &m) != EOF)
{
init();
for (int i = ; i <= n; i++)
scanf("%lld", &val[i]);
LL f, t;
for (int i = ; i <= m; i++)
{
scanf("%lld%lld", &f, &t);
addedge(f, t, val[t]);
cnt[t]++;
out[f]++;
}
printf("%lld\n", Topsort());
}
}
Test for Job 图上的动态规划(DAG)的更多相关文章
- 2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划
2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划 [Problem Description] 有向无环图中,有个机器人从\(1\)号节点出发,每天等概率的走到下 ...
- 第九章(二)DAG上的动态规划
DAG上的动态规划: 有向无环图上的动态规划是学习DP的基础,很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 1.没有明确固定起点重点的DAG模型: 嵌套矩形问题:有n个矩形,每个矩形可 ...
- 9.2 DAG上的动态规划
在有向无环图上的动态规划是学习动态规划的基础,很多问题都可以转化为DAG上的最长路,最短路或路径计数问题 9.2.1 DAG模型 嵌套矩形问题: 矩形之间的可嵌套关系是一种典型的二元关系,二元关系可以 ...
- DAG上的动态规划之嵌套矩形
题意描述:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽, 矩形(a,b)可以嵌套在矩形(c,d)当且仅当a<c且b<d, 要求选出尽量多的矩形排成一排,使得除了最后一个外, ...
- DAG 上的动态规划(训练指南—大白书)
有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.矩形嵌套 题目描述: ...
- DP入门(2)——DAG上的动态规划
有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.DAG模型 [嵌套矩形问题] 问题 ...
- 嵌套矩形——DAG上的动态规划
有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.非常多问题都能够转化为DAG上的最长路.最短路或路径计数问题. 题目描写叙述: 有n个矩形,每一个矩 ...
- DAG上的动态规划---嵌套矩形(模板题)
一.DAG的介绍 Directed Acyclic Graph,简称DAG,即有向无环图,有向说明有方向,无环表示不能直接或间接的指向自己. 摘录:有向无环图的动态规划是学习动态规划的基础,很多问题都 ...
- UVa 103 Stacking Boxes --- DAG上的动态规划
UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
随机推荐
- [转]查询表达式 (F#)
本文转自:http://msdn.microsoft.com/zh-cn/library/hh225374.aspx 查询表达式可以查询数据源并将数据是一种预期形式. 查询表达 ...
- Jquery操作常用表单元素
由于对前端的东西不是很熟练,导致jquery操作表单的东西总是忘记,每次用都要查一下,效率低下,记录下来,以便下次使用. CheckBox checkbox改变事件 $('#IsAllSearch') ...
- 初学Ajax
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...
- 在阿里云上搭建nginx + ThinkPHP 的实践
作为一个程序猿,理应用linux系统来作为平时的工作机环境,哎,之前倒是用过一段时间的linux,可惜后来换了本本,后来竟然没有保持,嗷嗷后悔中... 废话不多说,大家用windows的理由都一样,但 ...
- H.264学习笔记3——帧间预测
帧间预测主要包括运动估计(运动搜索方法.运动估计准则.亚像素插值和运动矢量估计)和运动补偿. 对于H.264,是对16x16的亮度块和8x8的色度块进行帧间预测编码. A.树状结构分块 H.264的宏 ...
- url传值的长度限制解决办法
今天写到两个页面传值,刚开始通过url上加参数进行传值, var strLink = "my.asp?str1=" + str1List + "&str2=&qu ...
- 解决QTreeView不能设置列宽的问题
转载请注明出处:http://www.cnblogs.com/dachen408/p/7206738.html 设置model之前: ui.treeView->setColumnWidth(0, ...
- CSS中的趣事之float浮动
浮动float一般跟left或是right: 特性: 1,包裹性:浮动文本类型时,需要指定宽度width,如果不指定,就会折叠到最小宽度: 2,浮动会影响别的元素: 3,子级浮动,会导致父级高度 ...
- 最好的Sublime Text插件集合
阅读目录 WebInspector Emmet Git GitGutter & Modific Sublimall AllAutocomplete SublimeREPL DocBlockr ...
- spring-shiro 配置
配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www ...