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
题意 求n个点 m条边 t时间内 从点1到点n经过地点最多的路径解析 dfs搜深度会超时 看了下题解 要用dp写 dp[i][j]表示从i点到n点经过j个点的最少时间
状态转移方程就是dp[i][j]=min(dp[k][j-1]+dis[i][k])k属于i能到达的相邻的点集,用个数组保存下路径。 AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 5e3+;
const int inf = 0x3f3f3f3f;
const double epx = 1e-;
const double pi = acos(-1.0);
const ll INF = 1e18;
const ll mod = ;
int n,m,t;
vector<int> mp[maxn];
vector<int> dis[maxn];
int vis[maxn],order[maxn][maxn],dp[maxn][maxn];
void dfs(int c)
{
vis[c]=;
if(c==n)
return;
for(int i=;i<mp[c].size();i++)
{
int k=mp[c][i];
int w=dis[c][i];
if(!vis[k])
dfs(k);
for(int j=;j<=n;j++)
{
if(dp[c][j]>dp[k][j-]+w)
{
dp[c][j]=dp[k][j-]+w;
order[c][j]=k;
}
}
}
}
int main()
{
cin>>n>>m>>t;
mem(vis,);
mem(dp,inf);
mem(order,);
for(int i=;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
mp[u].push_back(v);
dis[u].push_back(w);
}
dp[n][]=;
dfs();
int temp;
for(int i=n;i>=;i--)
{
if(dp[][i]<=t)
{
cout<<i<<endl;
temp=i;
break;
}
}
cout<<<<" ";
int nextt=order[][temp];
while(nextt!=n)
{
cout<<nextt<<" ";
nextt=order[nextt][--temp];
}
cout<<n<<endl;
}
Codeforces Round #374 (Div. 2) C DAG上dp的更多相关文章
- 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)
C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- 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 #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 #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 ...
随机推荐
- vue.js学习参考手册
参考手册 示例:www.51siyuan.cn/161.html
- 微信打开网址添加在浏览器中打开提示 http://caibaojian.com/weixin-tip.html
原文链接:http://caibaojian.com/weixin-tip.html#t2 使用微信打开网址时,无法在微信内打开常用下载软件,手机APP等.网上流传的各种微信打开下载链接,微信已更新基 ...
- [BZOJ1005][HNOI2008]明明的烦恼 数学+prufer序列+高精度
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int N; ...
- 【转】Nicescroll滚动条插件的用法
原网址:http://blog.csdn.net/mss359681091/article/details/52838179 Nicescroll滚动条插件是一个非常强大的基于JQUERY的滚动条插件 ...
- CentOS7.5搭建LAMP环境
导言 LAMP环境搭建,网上可以搜到很多的结果.为什么我还要整理一下呢,是因为有些网上给出的解决办法可能仅适用于某些特定的环境下,并不一定适用于所有出现问题的情况. 当然我写本篇的目的也不是保证所有的 ...
- 来自锐动天地的直播ios SDK
直播iOS SDK,可以在手机iOS端实时采集视频,同时在拍摄过程中支持多种实时滤镜效果,只要调用视频直播接口,通过3G.4G.WIFI等网络,推流发送给云端流媒体直播系统处理,并通过CDN视频加速分 ...
- fedora下yum安装gnome和kde桌面 (有问题 )
转自: http://linux.chinaunix.net/techdoc/system/2009/08/31/1133198.shtml 1.1 安装KDE桌面环境 yum groupins ...
- LeetCode887鸡蛋掉落——dp
题目 题目链接 你将获得 K 个鸡蛋,并可以使用一栋从 1 到 N 共有 N 层楼的建筑.每个蛋的功能都是一样的,如果一个蛋碎了,你就不能再把它掉下去,如果没有碎可以继续使用.你知道存在楼层 F , ...
- thinkphp5验证码处理
1.确定项目目录>vendor>topthink>think-captcha目录存在 2.在config中添加验证码配置 //验证码配置 'captcha' => [ // 验 ...
- CentOS7-Git安装以及使用
2018-09-14 Git安装 在bash终端中输入命令sudo yum install git回车. (出乎意料的顺利) 在随后出现的交互式对话中输入y即可. 随后,当任务执行完后,在bash中键 ...