Codeforces Round #374 (Div. 2) C(DAG上的DP)
3 seconds
256 megabytes
standard input
standard output
Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.
Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.
Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time units passing it.
The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000, 1 ≤ m ≤ 5000, 1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.
The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.
It is guaranteed, that there is at most one road between each pair of showplaces.
Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.
Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.
If there are multiple answers, print any of them.
4 3 13
1 2 5
2 3 7
2 4 8
3
1 2 4
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
4
1 2 4 6
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
3
1 3 5 思路:,
之前直接暴搜搜超时了,感觉时间复杂度减不下去,看了下题解,用的是dp,dp[i][j]代表从i点到n点需要经过j个点,
但我感觉和记忆化搜索的思维有点像像,都是储存各个点的最优状态,这样遍历到这个点的时候只要看下是否访问过了,
访问过了的话就更新一遍最优状态就可以了,这样就可以节省很多时间复杂度, 实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 5e3+;
#define ll long long
int n,m;
int T;
int dp[M][M],to[M][M],vis[M];
vector<int>g[M];
vector<int>q[M];
void dfs(int u){
vis[u] = ;
if(u==n) return ;
for(int i = ;i < g[u].size();i ++){
int v = g[u][i];
int d = q[u][i];
if(!vis[v])
dfs(v);
for(int j = ;j <= n;j ++){
if(dp[v][j-] + d < dp[u][j]){
dp[u][j] = dp[v][j-] + d;
to[u][j] = v;
}
}
}
} int main()
{
memset(dp,0x3f,sizeof(dp));
int u,v,x;
cin>>n>>m>>T;
for(int i = ;i < m;i ++){
cin>>u>>v>>x;
g[u].push_back(v);
q[u].push_back(x);
}
dp[n][] = ;
dfs();
for(int i = ;i <= n;i ++){
cout<<i<<" :";
for(int j = ;j <= n;j ++){
cout<<j<<" "<<dp[i][j]<<" ";
}
cout<<endl;
}
int ans = ;
for(int i = n;i >= ;i --){
//cout<<dp[1][i]<<" ";
if(dp[][i] <= T){
ans = i;
cout<<ans<<endl;
cout<<"1 ";
break;
}
}
int cur = ;
while(ans>){
cout<<to[cur][ans]<<" ";
cur = to[cur][ans--];
}
}
Codeforces Round #374 (Div. 2) C(DAG上的DP)的更多相关文章
- Codeforces Round #374 (Div. 2) C DAG上dp
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #374 (Div. 2)
A题和B题是一如既往的签到题. C题是一道拓扑序dp题,题意是给定一个DAG,问你从1号点走到n号点,在长度不超过T的情况下,要求经过的点数最多,换个思维,设dp[i][j]表示到i号点时经过j个点的 ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
随机推荐
- cloudstack 创建虚拟机失败
Trying to find a potenial host and associated storage pools from the suitable host/pool lists for th ...
- 关于C#中async/await中的异常处理(下)-(转载)
上一篇文章里我们讨论了某些async/await的用法中出现遗漏异常的情况,并且谈到该如何使用WhenAll辅助方法来避免这种情况.WhenAll辅助方法将会汇总一系列的任务对象,一旦其中某个出错,则 ...
- jQuery上传文件
1.引入资源 <script src="/yami/backend/backres/js/jquery.min.js"></script> <scri ...
- 怎样让oracle实验本在不做实验时性能提升——win7下举例
怎样让oracle实验本在不做实验时性能提升--win7下举例 型号:ThinkPad E431 系统:WIN7 实验使用的笔记本不使用数据库时.建议将oracle关闭,使其释放占用的资源. orac ...
- 现有工程中集成Cordova
cocoapods引入cordova源码 1.依赖Cordova和wk插件 pod 'Cordova' pod 'cordova-plugin-wkwebview-engine' 建立Cordova支 ...
- 20155325 Exp8 Web基础
实验要求 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. (2).Web前端javascipt(0 ...
- tensorflow batch
这两天一直在看tensorflow中的读取数据的队列,说实话,真的是很难懂.也可能我之前没这方面的经验吧,最早我都使用的theano,什么都是自己写.经过这两天的文档以及相关资料,并且请教了国内的师弟 ...
- python 回溯法 子集树模板 系列 —— 13、最佳作业调度问题
问题 给定 n 个作业,每一个作业都有两项子任务需要分别在两台机器上完成.每一个作业必须先由机器1 处理,然后由机器2处理. 试设计一个算法找出完成这n个任务的最佳调度,使其机器2完成各作业时间之和达 ...
- 一个Python开源项目-哈勃沙箱源码剖析(下)
前言 在上一篇中,我们讲解了哈勃沙箱的技术点,详细分析了静态检测和动态检测的流程.本篇接着对动态检测的关键技术点进行分析,包括strace,sysdig,volatility.volatility的介 ...
- 管理idea Open Recent
在微服务开发过程中,随着服务的增加,日常需要在各个服务之间切换,这样idea 的 Open Recent 功能就显得特别有用,但是过多的最近打开记录中包括已经删除的工程或者无用的工程导致影响开发时切换 ...