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. [BZOJ1088][SCOI2005]扫雷Mine DP

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1088 记录下每一个格子对应左边格子放的雷的情况,然后dp转移就好了. #include&l ...

  2. T4869 某种数列问题 (jx.cpp/c/pas) 1000MS 256MB

    题目描述 众所周知,chenzeyu97有无数的妹子(阿掉!>_<),而且他还有很多恶趣味的问题,继上次纠结于一排妹子的排法以后,今天他有非(chi)常(bao)认(cheng)真(zhe ...

  3. Android SDK镜像更新网速慢的解决问题

    通过更换代理解决 Android SDK 在线更新镜像服务器资源:大连东软信息学院镜像服务器地址:http://mirrors.neusoft.edu.cn 端口:80北京化工大学镜像服务器地址:IP ...

  4. H.264学习笔记4——变换量化

    A.变换量化过程总体介绍 经过帧内(16x16和4x4亮度.8x8色度)和帧间(4x4~16x16亮度.4x4~8x8色度)像素块预测之后,得到预测块的残差,为了压缩残差信息的统计冗余,需要对残差数据 ...

  5. opencv读图片错误,已解决

    could not loag image... terminate called after throwing an instance of 'cv::Exception' what(): OpenC ...

  6. pom.xml配置引用项目时不生效

    1 在项目pom.xml配置中引用项目A,但是编译时,取提数引起是B: 2 原因是:[Java Build Path - Projects] 引用的还是老的项目B,删除该引用即可解决.

  7. vlmcsd-1111-2017-06-17

    Source and binaries: http://rgho.st/6c6R7RwMZ   全部编译好了 https://www.upload.ee/files/7131474/vlmcsd-11 ...

  8. vue按需加载组件-webpack require.ensure

    使用 vue-cli构建的项目,在 默认情况下 ,执行 npm run build 会将所有的js代码打包为一个整体, 打包位置是 dist/static/js/app.[contenthash].j ...

  9. [实现] 利用 Seq2Seq 预测句子后续字词 (Pytorch)2

    最近有个任务:利用 RNN 进行句子补全,即给定一个不完整的句子,预测其后续的字词.本文使用了 Seq2Seq 模型,输入为 5 个中文字词,输出为 1 个中文字词.目录 关于RNN 语料预处理 搭建 ...

  10. 小程序调用支付报错:jsapi缺少参数: total_fee

    这种情况通常是因为在调用的时候,package参数有问题导致: wx.requestPayment中package参数必须是package:"prepay_id=wx21********** ...