POJ3249(DAG上的dfs)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 10567 | Accepted: 2482 |
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
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int MAXN=;
const LL INF=1LL<<;
int val[MAXN];
int n,m;
vector<int> arc[MAXN];
int deg[MAXN],vis[MAXN];
LL dp[MAXN];
void dfs(int u)
{
vis[u]=;
LL mx=-INF;
for(int i=;i<arc[u].size();i++)
{
int to=arc[u][i];
if(!vis[to])
{
dfs(to);
}
mx=max(dp[to],mx);
}
if(mx==-INF) mx=;
dp[u]=val[u]+mx;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++) arc[i].clear();
memset(dp,,sizeof(dp));
memset(deg,,sizeof(deg));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
}
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
arc[u].push_back(v);
deg[v]++;
}
LL res=-INF;
for(int i=;i<=n;i++)
{
if(deg[i]==)
{
dfs(i);
res=max(res,dp[i]);
}
}
printf("%lld\n",res);
}
return ;
}
POJ3249(DAG上的dfs)的更多相关文章
- 求DAG上两点的最短距离
Problem 给出一个不带边权(即边权为1)的有向无环图(unweighted DAG)以及DAG上两点s, t,求s到t的最短距离,如果无法从s走到t,则输出-1. Solution DFS,BF ...
- DAG上的DP
引例:NYOJ16 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可 ...
- VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]
传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #545 (Div. 2) E 强连通块 + dag上求最大路径 + 将状态看成点建图
https://codeforces.com/contest/1138/problem/E 题意 有n个城市(1e5),有m条单向边(1e5),每一周有d天(50),对于每个城市假如在某一天为1表示这 ...
- 【春训团队赛第四场】补题 | MST上倍增 | LCA | DAG上最长路 | 思维 | 素数筛 | 找规律 | 计几 | 背包 | 并查集
春训团队赛第四场 ID A B C D E F G H I J K L M AC O O O O O O O O O 补题 ? ? O O 传送门 题目链接(CF Gym102021) 题解链接(pd ...
- 9.2 DAG上的动态规划
在有向无环图上的动态规划是学习动态规划的基础,很多问题都可以转化为DAG上的最长路,最短路或路径计数问题 9.2.1 DAG模型 嵌套矩形问题: 矩形之间的可嵌套关系是一种典型的二元关系,二元关系可以 ...
- [CF225C] Barcode (简单DAG上dp)
题目链接:http://codeforces.com/problemset/problem/225/C 题目大意:给你一个矩阵,矩阵中只有#和.两种符号.现在我们希望能够得到一个新的矩阵,新的矩阵满足 ...
- UVa 103 Stacking Boxes --- DAG上的动态规划
UVa 103 题目大意:给定n个箱子,每个箱子有m个维度, 一个箱子可以嵌套在另一个箱子中当且仅当该箱子的所有的维度大小全部小于另一个箱子的相应维度, (注意箱子可以旋转,即箱子维度可以互换),求最 ...
- DAG上的动态规划之嵌套矩形
题意描述:有n个矩形,每个矩形可以用两个整数a.b描述,表示它的长和宽, 矩形(a,b)可以嵌套在矩形(c,d)当且仅当a<c且b<d, 要求选出尽量多的矩形排成一排,使得除了最后一个外, ...
随机推荐
- kubernetes 核心对象
Pods Pod是Kubernetes的基本操作单元,也是应用运行的载体.整个Kubernetes系统都是围绕着Pod展开的,比如如何部署运行Pod.如何保证Pod的数量.如何访问Pod等.另外,Po ...
- 微服务(MicroServices)
微服务Architecture(MicroServices) 微服务架构简单的定义 采用一组Service的方式来构建一个应用,服务独立部署在不同的进程(Container)中,不同Service通过 ...
- 2015年蓝桥杯C/C++ B组题目题解
1. 输入一个字符串,求它包含多少个单词.单词间以一个或者多个空格分开. 第一个单词前,最后一个单词后也可能有0到多个空格.比如:" abc xyz" 包含两个单词,"a ...
- Codeforces Round #368 (Div. 2) A , B , C
A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- C++两种字符串传参构造函数
第一种: #include"iostream" #include"string" using namespace std; class Motor{ prote ...
- MDX中Filter 与Exist的区别
获得一个集合,这个一般用来筛选出一个自定义的set,比如在中国的餐厅 该set返回所有MSDNteam下并且在Fact Thread度量上有记录的products 用Exists实现 sele ...
- SQL Server 性能优化之RML Utilities:快速入门(Quick Start)(1)
SQL Server 性能优化之RML Utilities:快速入门(Quick Start)(1) 安装Quick Start工具 RML(Replay Markup Language)是MS ...
- Mac开机启动
1. Finder打开资源库的LaunchAgents目录. 打开Finder,按⇧⌘G,输入 /Library/LaunchAgents/ 以及 ~/Library/LaunchAgents/ 2. ...
- hdu 2087 kmp
http://acm.hdu.edu.cn/showproblem.php?pid=2087 算是模板题吧,找到一个子串之后将模板串指针归零否则会重复计算. #include<bits/stdc ...
- nohup+命令+& 【退出终端后,程序依然在后台运行】
[ 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以忽略所有挂断(SIGHUP)信号,在你退出帐户/关闭终端之后继续运行相应的进程. nohup ...