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个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
随机推荐
- AJPFX关于多态的应用
要求设计一个方法,要求此方法可以接受A类的任意子类对象,并调用方法,此时,如果不使用对象多态性,那代码肯定会类似如下 class A{ // 定义类A publi ...
- JavaScript实现JQuery的功能
- idea 调试工具的使用
原文:https://blog.csdn.net/hao_hl1314/article/details/53120918 Intellij IDEA Debug调试区工具的使用方法 快捷键F9 ...
- PHP文件及目录考察点
文件读取/写入操作 fopen()函数 用来打开一个文件,打开时需要指定打开模式 打开模式 模式 |作用 --- |--- 'r' |只读方式打开,将文件指针指向文件头. 'r+' |读写方式打开,将 ...
- MySQL性能优化之max_connections配置
MySQL的最大连接数,增加该值增加mysqld 要求的文件描述符的数量.如果服务器的并发连接请求量比较大,建议调高此值,以增加并行连接数量,当然这建立在机器能支撑的情况下,因为如果连接数越多,介于M ...
- 谷歌全屏脚本 start chrome.exe --kiosk http://www.baidu.com
start chrome.exe --kiosk http://www.baidu.com
- EditControl 限制输入文本的三种方法
下边是三种限制编辑框输入内容的方法.在VS里建立基于对话框的应用程序CMyEdit,打开资源视图,删除对话框上自带的确定和取消按钮.然后添加一编辑框控件,将其ID修改为IDC_MY_EDIT,通过类向 ...
- java混淆工具Jocky和Proguard
java混淆工具有很多种,这里介绍Jocky和Proguard 一:Jocky是金蝶中间件技术领袖袁红岗先生的个人作品(旧有名称JOC).原本是方便Apusic 应用服务器的开发,现在开放出来,供大家 ...
- 第3节 mapreduce高级:12、mapreduce相关的参数调整
5.1 多job串联 一个稍复杂点的处理逻辑往往需要多个mapreduce程序串联处理,多job的串联可以借助mapreduce框架的JobControl实现 示例代码: ControlledJob ...
- 精准判断是360、IE和其他浏览器
function myexplorer(){ var explorer = window.navigator.userAgent; if (!!window.ActiveXObject || &quo ...