C. Journey
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

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.

Examples
input
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6
input
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
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的更多相关文章

  1. 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

  2. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  3. 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 ...

  4. Codeforces Round #374 (Div. 2)

    A题和B题是一如既往的签到题. C题是一道拓扑序dp题,题意是给定一个DAG,问你从1号点走到n号点,在长度不超过T的情况下,要求经过的点数最多,换个思维,设dp[i][j]表示到i号点时经过j个点的 ...

  5. 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 ...

  6. 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 ...

  7. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  8. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  9. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

随机推荐

  1. SharePoint2013升级SP1后,运行配置向导报错:未注册sharepoint服务

    SharePoint Server 2013 升级SP1后,需要重新运行配置向导,但是运行过程中报错:未注册sharepoint服务. 日志详细错误信息: 已引发类型为 Microsoft.Share ...

  2. hihocoder offer收割编程练习赛11 D 排队接水

    思路: 莫队算法+树状数组. 莫队算法的基本思想是对大量要查询的区间进行离线处理,按照一定的顺序计算,来降低复杂度.概括来说,我们在知道了[l, r]的解,并且可以通过一个较低的复杂度推出[l - 1 ...

  3. 12 DOM操作应用

    1.创建子元素oLi=document.creatElement('li') 2.将元素附给父级元素oUl.appendChild(oLi) 3.将元素插入到父级元素里的第一位子元素之前oUl.ins ...

  4. Android开发中使用startActivityForResult()方法从Activity A跳转Activity B出现B退出时A也同时退出的解决办法

    最近一个 App 中用到了 startActivityForResult() 方法,使用的时候却出现了一些问题,比如我在 Activity A 中调用该方法向 Activity B 中跳转,如果 B  ...

  5. 老潘 - ListView分析 - 学以致用篇(一)

    ListView分析学以致用篇(1) 在我们查看别人的博客的时候,一个人是一个风格的.先说下我的风格,我喜欢思想类比,然后介绍知识,不太喜欢填鸭式的灌输.如果只是想单纯的从我的博客中直接看到代码,我个 ...

  6. 机器学习-牛顿方法&指数分布族&GLM

    本节内容 牛顿方法 指数分布族 广义线性模型 之前学习了梯度下降方法,关于梯度下降(gradient descent),这里简单的回顾下[参考感知机学习部分提到的梯度下降(gradient desce ...

  7. iOS Programming Introduction to Auto Layout 自动布局

    iOS Programming Introduction to Auto Layout   自动布局 A single application that runs natively on both t ...

  8. quazip非静态成员。。错误

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7147155.html 问题:quazip非静态成员..错误 解决方案:quazip_global.h  第42 ...

  9. Leetcode_638.Shopping Offers

    https://leetcode.com/problems/shopping-offers/ In LeetCode Store, there are some kinds of items to s ...

  10. Duplicate fragment name ERROR Jetty Maven Plugin

    http://stackoverflow.com/questions/5802096/duplicate-fragment-name-error-jetty-maven-plugin 4down vo ...