【codeforces 721C】【DAG 按照拓扑排序记忆化DFS】C. Journey 【DAG图,5000个点,5000条边 让你求从1到n的路径长度不超过T中经过点数最多的一条 】
来源:互联网 发布:小语网络加速器 编辑:程序博客网 时间:2024/05/22 10:04
传送门:C. Journey
描述:
题意:
DAG图,5000个点,5000条边让你求从1到n的路径长度不超过T中经过点数最多的一条
思路:
f[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的最小距离
nxt[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的后继 ,这里用short可以优化不少内存
然后 按照拓扑排序记忆化搜索一下
如果我们有x的后继为y
那么我们肯定可以对y做dfs,在对y做dfs的时候,显然不会走到x(DAG图)
换而言之,y的所有可能的前驱(包括前驱的前驱)都不会影响到dfs(y)的值
于是,这里就保证了该dp的正确拓扑序,也就可以在O(n^2)的复杂度内出解。
代码:
#include <bits/stdc++.h>#define pr(x) cout << #x << "= " << x << " " ;#define pl(x) cout << #x << "= " << x << endl;#define ll __int64#define mst(ss,b) memset(ss,b,sizeof(ss));using namespace std;const int N=5050;const int inf=0x3f3f3f3f;int n, m, T;int f[N][N];//f[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的最小距离short nxt[N][N];//nxt[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的后继vector< pair<int,int> >a[N];bool vis[N];void dfs(int x){ if (vis[x])return; vis[x] = 1; for (auto it : a[x]){ dfs(it.first); for (int i = 2; i <= n; ++i){ int dis = f[it.first][i - 1] + it.second; if (dis < f[x][i]){ f[x][i] = dis; nxt[x][i] = it.first; } } }}void print(){ for (int i = n; ; --i)if (f[1][i] <= T){ printf("%d\n", i); int x = 1; printf("%d ", x); while (x != n){ x = nxt[x][i--]; printf("%d ", x); }puts(""); break; }}int main(){ while(~scanf("%d%d%d",&n, &m, &T)){ for (int i = 1; i <= n; ++i)a[i].clear(), vis[i] = 0; for (int i = 1; i <= m; ++i){ int x, y, z; scanf("%d%d%d", &x, &y, &z); if (y == 1 || x == n)continue; a[x].push_back({ y,z }); } mst(f, inf); f[n][1] = 0; vis[n] = 1; dfs(1); print(); } return 0;}
0 0
- 【codeforces 721C】【DAG 按照拓扑排序记忆化DFS】C. Journey 【DAG图,5000个点,5000条边 让你求从1到n的路径长度不超过T中经过点数最多的一条 】
- 【Codeforces Round 374 (Div 2)C】【DAG上的DP】Journey 有向无环图从1到n时刻T内的最多经过点数
- Codeforces #374(Div.2)C. Journey【DAG图上的拓扑排序+Dp】
- 【FOJ2210 11月月赛F】【DFS or 拓扑排序】攻占计划 n个点m条边DAG破坏一个点使得不可达点数尽可能多
- Codeforces 721C. Journey(DAG上的DP)
- 求一条直线经过一个直角坐标系里m个点最多点的点数
- http://codeforces.com/contest/721/problem/C Journey(DAG上的dp)
- codeforce 721C Journey (DAG上的dp)
- DAG图的拓扑排序 python
- [容易] DAG的拓扑排序
- CF 721C DAG上的dp
- Codeforces Round #374 (Div. 2) 721C Journey DAG上DP
- 求不超过N的正整数中因子最多的数
- DAG图与拓扑排序
- codeforces 721C DAG上dp
- 在二维平面上,有一些点。请找出经过点数最多的那条线
- (求DAG图最长路)拓扑排序模板
- 用拓扑排序+动态规划的方法输出DAG的所有可能的路径
- MVC --------Android
- Codeforces Round 374 div2
- 一天一篇批处理之——Part3:复制/移动文件命令move,copy,xcopy
- HTML5之WebSoket——html5+java servlet实现
- phpstorm激活方法
- 【codeforces 721C】【DAG 按照拓扑排序记忆化DFS】C. Journey 【DAG图,5000个点,5000条边 让你求从1到n的路径长度不超过T中经过点数最多的一条 】
- HDU 5833 Zhu and 772002(异或方程组高斯消元)
- LeetCode 405 Convert a Number to Hexadecimal
- 适配器模式
- (Java)LeetCode-50. Pow(x, n)
- Python:class类
- forward和param动作
- linux 终端命令大全
- 动态链接库的使用