洛谷 P1137 旅行计划 (拓扑排序+dp)
在DAG中,拓扑排序可以确定dp的顺序
把图的信息转化到一个拓扑序上
注意转移的时候要用边转移
这道题的dp是用刷表法
#include<bits/stdc++.h>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std; const int MAXN = 1e5 + ;
struct Edge{ int to, next; };
Edge e[MAXN << ];
int head[MAXN], d[MAXN];
int topo[MAXN], dp[MAXN];
int n, m, cnt, tot; void AddEdge(int from, int to)
{
e[tot] = Edge{to, head[from]};
head[from] = tot++;
} void read(int& x)
{
int f = ; x = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -; ch = getchar(); }
while(isdigit(ch)) { x = x * + ch - ''; ch = getchar(); }
x *= f;
} void toposort()
{
queue<int> q;
_for(i, , n)
if(!d[i])
q.push(i);
while(!q.empty())
{
int u = q.front(); q.pop();
topo[++cnt] = u;
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
if(--d[v] == ) q.push(v);
}
}
} int main()
{
memset(head, -, sizeof(head)); tot = ;
read(n); read(m); _for(i, , m)
{
int u, v;
read(u); read(v);
AddEdge(u, v);
d[v]++;
} toposort();
_for(i, , n) dp[i] = ;
_for(i, , n)
{
int u = topo[i];
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
dp[v] = max(dp[v], dp[u] + );
}
}
_for(i, , n) printf("%d\n", dp[i]); return ;
}
还可以用记忆化搜索
#include<bits/stdc++.h>
#define REP(i, a, b) for(register int i = (a); i < (b); i++)
#define _for(i, a, b) for(register int i = (a); i <= (b); i++)
using namespace std; const int MAXN = 1e5 + ;
struct Edge{ int to, next; };
Edge e[MAXN << ];
int head[MAXN], dp[MAXN];
int n, m, cnt, tot; void AddEdge(int from, int to)
{
e[tot] = Edge{to, head[from]};
head[from] = tot++;
} void read(int& x)
{
int f = ; x = ; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -; ch = getchar(); }
while(isdigit(ch)) { x = x * + ch - ''; ch = getchar(); }
x *= f;
} int dfs(int u)
{
if(dp[u] != -) return dp[u];
dp[u] = ;
for(int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
dp[u] = max(dp[u], dfs(v) + );
}
return dp[u];
} int main()
{
memset(head, -, sizeof(head)); tot = ;
read(n); read(m); _for(i, , m)
{
int u, v;
read(u); read(v);
AddEdge(v, u);
} memset(dp, -, sizeof(dp));
_for(i, , n) printf("%d\n", dfs(i)); return ;
}
洛谷 P1137 旅行计划 (拓扑排序+dp)的更多相关文章
- 洛谷P1137 旅行计划
P1137 旅行计划 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制 ...
- 洛谷P1137 旅行计划 解题报告(拓扑排序+DP)
我看了一下其他大佬的题解,大部分都是拓扑排序加上DP.那么我想有的人是不明白为什么这么做的,拓扑排序有什么性质使得可以DP呢?下面我就提一下. 对一个有向无环图(Directed Acyclic Gr ...
- 洛谷 P1137 旅行计划
旅行计划 待证明这样dp的正确性. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- 【洛谷1685】游览 拓扑排序+DP
题目描述 顺利通过了黄药师的考验,下面就可以尽情游览桃花岛了! 你要从桃花岛的西头开始一直玩到东头,然后在东头的码头离开.可是当你游玩了一次后,发现桃花岛的景色实在是非常的美丽!!!于是你还想乘船从桃 ...
- 洛谷——P1137 旅行计划
https://www.luogu.org/problem/show?pid=1137 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出 ...
- 洛谷p1137旅行计划
题面 关于拓扑排序 因为这好几次考试的题目里都有在DAG中拓扑排序求最长/短路 txt说它非常的好用 就找了个题做了下 拓扑排序就是寻找图中所有的入度为零的点把他入队 然后再枚举它所有的连到的点,只要 ...
- Luogu1137 旅行计划(拓扑排序)
题目传送门 拓扑排序板子题,模拟即可. 代码 #include<cstdio> #include<iostream> #include<cmath> #includ ...
- 洛谷P2597 [ZJOI2012] 灾难 [拓扑排序,LCA]
题目传送门 灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. ...
- 洛谷P2296 寻找道路 [拓扑排序,最短路]
题目传送门 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
随机推荐
- maven 自建库
maven repository 标签: mavenjarxmlserver工具磁盘 2009-11-26 10:56 42322人阅读 评论(7) 收藏 举报 目录(?)[+] 什么是Mav ...
- 基于UEFI和GPT模式下U盘安装windows8.1和Linux双启动教程
首先作以下准备: 1.一个8G以上的U盘,用的时候会格式化,建议为空 2.分区助手软件,官网下载链接 3.一个linux系统,这里用同学推荐的Fedora 26,官网下载链接 4.rufus 创建U盘 ...
- 架构-Eureka:第一个方法
ylbtech-架构-Eureka:第一个方法 工程介绍 Spring Cloud 工程目录 model registry-center Servers tzxyfx tzxyfx-provider ...
- 谈谈cookie的弊端
一.cookie虽然在数据在客户端持久存储提供了方便,但是分担了服务器数据传输的负担,还是存在很大的局限性的. 局限性: (1)在特定的域名下最多存储20个cookie. 浏览器版本 ...
- AUTOCAD2013 以上利用ACCORECONSOLE+ SCR后台批量清理图纸
无意中浏览到南胜大神的博客,https://www.cnblogs.com/NanShengBlogs/p/10957489.html 受此启发,特意改装此方法用于批量清理图纸,效果极佳. 详细介绍详 ...
- JS——BOM操作(基本用法与实现:open()、close()、scrollTop等了解)
(1)window.open() 定义和用法 open() 方法用于打开一个新的浏览器窗口或查找一个已命名的窗口 语法 window.open(URL,name,specs,replace) [默认填 ...
- Java中的命名规范到底是怎样的
内容摘要:命名规范二,java中的方法名,对象名和字段名的第一个单词的首写字母应该小写,而后面的每个单词的首字母都应该小写 要想将java基础学的十分的牢固就必须将java中的命名规范掌握好了.俗话说 ...
- Matplotlib库常用函数大全
Python之Matplotlib库常用函数大全(含注释) plt.savefig(‘test’, dpi = 600) :将绘制的图画保存成png格式,命名为 test plt.ylabel(‘Gr ...
- 使用Axis2方式发布webService实例说明
1.简单的pojo方式: 不需要写配置文件,直接把class文件拷贝到axis2的WEB-INF目录下的poji文件夹下即可.但其局限性表现在,实现类不能有包声明,这在实际开发过程中使用较少,这里不做 ...
- 【Oracle】开、关、删归档日志(archivelog)
归档日志:当redo日志发生切换时,归档进程(ARCn)会把redo日志自动归档,产生归档日志. 1.启动归档模式 –确定数据库为非归档模式 SYS@LGR> archive log list; ...