在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)的更多相关文章

  1. 洛谷P1137 旅行计划

    P1137 旅行计划 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制 ...

  2. 洛谷P1137 旅行计划 解题报告(拓扑排序+DP)

    我看了一下其他大佬的题解,大部分都是拓扑排序加上DP.那么我想有的人是不明白为什么这么做的,拓扑排序有什么性质使得可以DP呢?下面我就提一下. 对一个有向无环图(Directed Acyclic Gr ...

  3. 洛谷 P1137 旅行计划

    旅行计划 待证明这样dp的正确性. #include <iostream> #include <cstdio> #include <cstring> #includ ...

  4. 【洛谷1685】游览 拓扑排序+DP

    题目描述 顺利通过了黄药师的考验,下面就可以尽情游览桃花岛了! 你要从桃花岛的西头开始一直玩到东头,然后在东头的码头离开.可是当你游玩了一次后,发现桃花岛的景色实在是非常的美丽!!!于是你还想乘船从桃 ...

  5. 洛谷——P1137 旅行计划

    https://www.luogu.org/problem/show?pid=1137 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出 ...

  6. 洛谷p1137旅行计划

    题面 关于拓扑排序 因为这好几次考试的题目里都有在DAG中拓扑排序求最长/短路 txt说它非常的好用 就找了个题做了下 拓扑排序就是寻找图中所有的入度为零的点把他入队 然后再枚举它所有的连到的点,只要 ...

  7. Luogu1137 旅行计划(拓扑排序)

    题目传送门 拓扑排序板子题,模拟即可. 代码 #include<cstdio> #include<iostream> #include<cmath> #includ ...

  8. 洛谷P2597 [ZJOI2012] 灾难 [拓扑排序,LCA]

    题目传送门 灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. ...

  9. 洛谷P2296 寻找道路 [拓扑排序,最短路]

    题目传送门 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...

随机推荐

  1. HDU 2457

    直接从root遍历扩展DP,当扩展到的字母和字符串字母相同时,不用修改,不同时,要求修改加1 注意不要扩展危险结点. #include <iostream> #include <cs ...

  2. HDU 2912

    直线关于球的多次反射,求最后一次反射点 #include <iostream> #include <cstdio> #include <cstring> #incl ...

  3. quick-cocos2d-x游戏开发【9】——单点触摸

    quick的触摸机制,我想廖大已经在这篇文章里说的非常清楚了.我们这些小辈们就是在他的基础上完备一下,说说使用方法就能够了.嘿嘿. 在2.2.3之前的版本号(不包含2.2.3).触摸机制和廖大在那篇文 ...

  4. 具体解释Hibernate中的事务

    1.前言 上一篇博客解说了Hibernate中的一级缓存,属于Session级别的.这篇博客解说一下Hibernate中的事务机制. 有关事务的概念.请參照通俗易懂数据库中的事务.  2.怎样处理Hi ...

  5. JPA相关注解

    JPA注解 一.基本注解 1.表相关   @Entity   仅仅要加了这个注解就具备了表和实体的映射关系,表名就是实体名   @Table(name="表名")    一般和实体 ...

  6. JavaScript 和Ajax跨域问题

    json格式: { "message":"获取成功", "state":"1", "result": ...

  7. The current .NET SDK does not support targeting .NET Core 2.1. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 2.1.

    C:\Program Files\dotnet\sdk\2.1.4\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.TargetFrameworkInferenc ...

  8. 【POI 2007】 山峰和山谷

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1102 [算法] 广度优先搜索 [代码] #include<bits/stdc+ ...

  9. 杂项-SpringEureka:笔记-1

    ylbtech-杂项-SpringEureka:笔记-1 1.返回顶部 1. THE SELF PRESERVATION MODE IS TURNED OFF.THIS MAY NOT PROTECT ...

  10. js设计模式-工厂模式(抽象工厂)

    场景:定义一个抽象类 AbsProducer(生产商),该生产商有两个行为,一个生产,一个出售,其中生产方法为抽象方法,由具体的厂家(工厂)去实现,出售的产品均是电子产品(返回的对象为电子产品对象,即 ...