Codeforces #374 div2 C Dp
来源:互联网 发布:java 写成绩划分 编辑:程序博客网 时间:2024/06/05 23:55
题目链接
题意
从1到n的路径中,时间不能超过T, 经过的点的最多的路径输出。
解析
用DP(动态规划),
代码
#include<iostream> #include<cstdio> #include<ctime> #include<cstring> #include<cstdlib> #include<vector> #include <algorithm>#include <queue>const int maxn = 5000+10;using namespace std; typedef long long LL;using namespace std;const int inf = 0x3f3f3f3f;typedef pair<int, int> P;int dp[maxn][maxn];int path[maxn][maxn];vector<int>ans;vector<P>g[maxn];int main() { int n, m , T; scanf("%d%d%d", &n, &m, &T); for (int i=0; i<m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); g[u].push_back(make_pair(v, w)); } memset(dp, inf, sizeof(dp)); dp[1][1] = 0; memset(path, -1, sizeof(path)); for (int i=2; i<=n; i++) { for (int j=1; j<=n; j++) { for (int k=0; k<g[j].size(); k++) { int v = g[j][k].first; int w = g[j][k].second; if (dp[i][v] >= dp[i-1][j]+w) { path[i][v] = j; dp[i][v] = dp[i-1][j]+w; } } } } int num; for (int i=1; i<=n; i++) if (dp[i][n] <= T) num = i; int t = num; for (int fa = n; fa!=-1; fa=path[t--][fa]) ans.push_back(fa); cout<<num<<endl; for (int i=(int)ans.size()-1; i>=0; i--) printf("%d ", ans[i]); printf("\n"); return 0;}
0 0
- Codeforces #374 div2 C Dp
- codeforces round367 div2.C (DP)
- Codeforces#374(div2)- C. Journey(二分答案+dp)
- codeforces #round363 div2.C-Vacations (DP)
- CodeForces 369 div2 C Coloring Trees DP
- CodeForces Round#229 DIV2 C 递推DP
- Codeforces Round #267 Div2 C George and Job --DP
- codeforces 789 div2 题解(AB水题,C dp,D图论)
- codeforces #78 div2 C
- codeforces 148 div2 C
- codeforces #169 div2 C
- codeforces 170 div2 C
- codeforces 178div2 C
- Codeforces Round225 DIV2 C
- codeforces 256 div2 C
- Codeforces #263 Div2 C
- codeforces 294 div2.C
- CODEFORCES #339 div2 C
- 最大子数列和的问题求解
- 最大密度闭合子图
- 1134 最长递增子序列
- MapReduce作业运行全貌
- 利用css和JSTL以及EL表达式生成颜色不一样的表格
- Codeforces #374 div2 C Dp
- 插入排序(升序)
- C++ strlen()+1问题
- struts2总结
- ac自动机静态比动态快
- dp和px、sp和px的相互转化的工具类
- 【codeofrces 722D】【贪心 STL维护】D. Generating Sets 【X集合的数 可以有两种方式变换Y 现在给你一个集合Y 求X 且这个X的里最大的数尽可能小】
- python中的try/except/else/finally语句
- springmvc 通过aop方式进行登录控制