Test for Job
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 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

Source

 
按照拓扑序列进行DP
#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)的更多相关文章

  1. 2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划

    2019-ACM-ICPC-南京区网络赛-D. Robots-DAG图上概率动态规划 [Problem Description] ​ 有向无环图中,有个机器人从\(1\)号节点出发,每天等概率的走到下 ...

  2. 第九章(二)DAG上的动态规划

    DAG上的动态规划: 有向无环图上的动态规划是学习DP的基础,很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 1.没有明确固定起点重点的DAG模型: 嵌套矩形问题:有n个矩形,每个矩形可 ...

  3. 9.2 DAG上的动态规划

    在有向无环图上的动态规划是学习动态规划的基础,很多问题都可以转化为DAG上的最长路,最短路或路径计数问题 9.2.1 DAG模型 嵌套矩形问题: 矩形之间的可嵌套关系是一种典型的二元关系,二元关系可以 ...

  4. DAG上的动态规划之嵌套矩形

    题意描述:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽, 矩形(a,b)可以嵌套在矩形(c,d)当且仅当a<c且b<d, 要求选出尽量多的矩形排成一排,使得除了最后一个外, ...

  5. DAG 上的动态规划(训练指南—大白书)

    有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.矩形嵌套 题目描述:       ...

  6. DP入门(2)——DAG上的动态规划

    有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.很多问题都可以转化为DAG上的最长路.最短路或路径计数问题. 一.DAG模型 [嵌套矩形问题] 问题 ...

  7. 嵌套矩形——DAG上的动态规划

    有向无环图(DAG,Directed Acyclic Graph)上的动态规划是学习动态规划的基础.非常多问题都能够转化为DAG上的最长路.最短路或路径计数问题. 题目描写叙述: 有n个矩形,每一个矩 ...

  8. DAG上的动态规划---嵌套矩形(模板题)

    一.DAG的介绍 Directed Acyclic Graph,简称DAG,即有向无环图,有向说明有方向,无环表示不能直接或间接的指向自己. 摘录:有向无环图的动态规划是学习动态规划的基础,很多问题都 ...

  9. UVa 103 Stacking Boxes --- DAG上的动态规划

    UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...

随机推荐

  1. Xml学习笔记(1)

    不同的xml文档构可能要用到不同的方法进行解析这里用到的是例如<student name="张三" id="1" sex="男"/&g ...

  2. Android EventBus3.0详解

    修改日志 -- 添加索引部分得细节,添加kotlin的支持方式 https://www.jianshu.com/p/31e3528ca7e5

  3. Oracle数据库的SQL语句之完整性约束——基础篇

    SELECT * FROM tb_clazz;SELECT * FROM tb_student; INSERT INTO tb_clazz(code,NAME,bzr) VALUES('1401',' ...

  4. 谈谈如何学习Linux操作系统

     献给初学者:为了能把这篇不错的文章分享给大家.所以请允许我暂时用原创的形式展现给大家. @hcy 更多资源:http://blog.sina.com.cn/iihcy 一. 选择适合自己的linux ...

  5. Objective-C 是动态语言

    Objective-C 的动态性是由 runtime 相关的库赋予的. 当然其他语言也完全可以运行在一个 Runtime 库上而获得动态性,由于多数高级语言的诞生都对应着一种编译器,因此将编译器的特性 ...

  6. -- HTML标记大全参考手册[推荐]

    --  HTML标记大全参考手册[推荐]总类(所有HTML文件都有的) 文件类型 <HTML></HTML> (放在档案的开头与结尾) 文件主题 <TITLE>&l ...

  7. jQuery-图片的放大镜显示效果方法封装

    (function($){ $.fn.jqueryzoom = function(options){ var settings = { xzoom: 200, //zoomed width defau ...

  8. Class加载顺序

    原文:https://blog.saymagic.cn/2017/07/01/class-common-question.html 类的初始化顺序是怎样的? 我们尝试从class文件中找到答案.来看这 ...

  9. C++操作MYSQL遇到的一些问题

    首先 我使用的是 vcpkg<不知道的可以进行百度 可以剧透一下,这个对Visual Studio使用一些C++的轮子太方便了, 上面是我装的一些库<大大安利vcpkg 安装时一定要使用p ...

  10. crm项目之stark组件前戏(二)

    stark组件的设计主要来源于django中admin的功能,在django admin中只需要将模型表进行注册,就可以在页面对该表进行curd的动作,那么django admin是如何做的呢? 在d ...