题目链接:http://codeforces.com/contest/721/problem/C

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 1 to 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 Ttime 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,最多可以经过多少个顶点?

其中,构成的图中不会出现环,且题目至少有一个答案。

题解:

一开始以为是最短路径,很显然不是,因为题目求的不是最短路,而是在限定的起点、终点和距离的情况下,最多能经过多少个点。

然后想到应该可以用DP:

dp[len][v]:从顶点1开始,途经len个顶点(包括起点终点),终点为v所花费的最短距离。

枚举len*枚举边:在已有的dp[len-1][u]的基础上,再得出dp[len][v], 很有DP的味道。

(注:用vector存顶点之间的关系时,枚举边 = 枚举起点*枚举终点)

保存路径:

一开始是用1维数组fa[]来保存。后来发现,假设当前顶点为v,用一维存的话,存的fa[v]仅仅是路径所能达到最大时的fa[v],而之前长度较小的路径的fa[v]会被新的fa[v]给覆盖掉。因此需要开二维数组fa[len][v]:记录在路径长度为len时,v的前一个顶点。

注意点:

if(dp[len-1][u]==INF) continue;   因为当dp[len-1][u]不合法时(其值设为2e9), 如果直接把他与w(最大为1e9)相加, 结果为3e9,超出了int的范围了。所以以后还是先判断其值是否合法或存在,然后再进项操作。

类似的DP:http://blog.csdn.net/dolfamingo/article/details/71024194

1.枚举长度*枚举起点*枚举终点:

 #include <bits/stdc++.h>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; struct node
{
int v, w;
}; int n,m,T;
vector<node>G[maxn];
int dp[maxn][maxn], fa[maxn][maxn]; void init()
{
scanf("%d%d%d",&n,&m,&T); for(int i = ; i<=n; i++)
G[i].clear(); for(int i = ; i<=m; i++)
{
int u;
node e;
scanf("%d%d%d",&u,&e.v,&e.w);
G[u].push_back(e);
} ms(fa,);
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
dp[i][j] = INF;
dp[][] = ;
} void prt(int len, int u)
{
if(len>)
prt(len-, fa[len][u]); printf("%d ",u);
} void solve()
{
int k;
for(int len = ; len<=n; len++)
{
for(int u = ; u<n; u++)
{
for(int i = ; i<G[u].size(); i++)
{
int v = G[u][i].v;
int w = G[u][i].w; if(dp[len-][u]==INF) continue; //少了这步,如果继续用int,会溢出,因为INF+1e9 int tmp = dp[len-][u] + w;
if(tmp<=T && dp[len][v]>tmp)
{
dp[len][v] = tmp;
fa[len][v] = u;
}
}
}
if(dp[len][n]!=INF)
k = len;
} printf("%d\n",k);
prt(k,n); putchar('\n'); } int main()
{
init();
solve();
return ;
}

2.枚举长度*枚举边:

 #include <bits/stdc++.h>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = +; struct node
{
int u, v, w;
void read()
{
scanf("%d %d %d",&u, &v, &w);
}
}edge[maxn]; int n,m,T;
int dp[maxn][maxn], fa[maxn][maxn]; void init()
{
scanf("%d%d%d",&n,&m,&T);
for(int i = ; i<=m; i++)
edge[i].read(); ms(fa,);
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
dp[i][j] = INF;
dp[][] = ;
} void prt(int len, int u)
{
if(len>)
prt(len-, fa[len][u]); printf("%d ",u);
} void solve()
{
int k;
for(int len = ; len<=n; len++)
{
for(int i = ; i<=m; i++)
{
int u = edge[i].u;
int v = edge[i].v;
int w = edge[i].w; if(dp[len-][u]==INF) continue; //少了这步,如果继续用int,会溢出,因为INF+1e9 int cost = dp[len-][u] + w;
if(cost<=T && cost<dp[len][v])
{
dp[len][v] = cost;
fa[len][v] = u;
}
} if(dp[len][n]!=INF)
k = len;
} printf("%d\n",k);
prt(k,n); putchar('\n');
} int main()
{
init();
solve();
return ;
}

Codeforces Round #374 (Div. 2) C. Journey —— DP的更多相关文章

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

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

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

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

  3. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

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

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

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

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

  7. Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

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

  9. Codeforces Round #374 (div.2)遗憾题合集

    C.Journey 读错题目了...不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图) 并且这题因为要求路径点尽可能多 其实可以规约为限定路径长的拓扑排序,不一定要用最短路做 #prag ...

随机推荐

  1. ASP.NET Core 中间件基本用法

    ASP.NET Core 中间件 ASP.NET Core的处理流程是一个管道,而中间件是装配到管道中的用于处理请求和响应的组件.中间件按照装配的先后顺序执行,并决定是否进入下一个组件.中间件管道的处 ...

  2. Java集合——概述

    Java集合——概述 摘要:本文主要介绍了几种集合类型以及有关的一些知识点. 集合类图 类图 类图说明 所有集合类都位于java.util包下.Java的集合类主要由两个接口派生而出:Collecti ...

  3. webpack 学习笔记 03 Code Splitting

    Introduction 对于较大的web 应用来说,将所有的代码文件压缩成一个文件是不合适的,在部分代码文件只有特殊情况下才被需要的情况下,这无疑是一种浪费.webpack 提供了讲代码文件分块的能 ...

  4. 邁向IT專家成功之路的三十則鐵律 鐵律十三:IT人理財之道-知足

    身為一位專業的IT人士,工作上不僅要做到滿足興趣與專業熱忱,當然也要做到能夠滿足荷包.現代人賺錢不是問題,但花錢卻出了很大問題,親愛的IT朋友們,請不要將您辛苦賺來的錢花在想要的東西上,實際上需要的卻 ...

  5. can-i-win(好)

    https://leetcode.com/problems/can-i-win/ package com.company; import java.util.*; class Solution { / ...

  6. 高仿QQ6.0側滑菜单之滑动优化(二)

    好了,昨天已经实现了高仿QQ6.0的側滑大致框架.如有兴趣.能够去看下仿QQ6.0側滑之ViewDragHelper的使用(一) 可是之前的实现.仅仅是简单的能够显示和隐藏左側的菜单,可是特别生硬,并 ...

  7. C语言-回溯例1

    回溯法解N皇后问题 1,代码分析: 使用一个一维数组表示皇后的位置 其中数组的下标表示皇后所在的行 数组元素的值表示皇后所在的列 这样设计的棋盘,所有皇后必定不在同一行 假设前n-1行的皇后已经按照规 ...

  8. CentOS minimal 安装ssh 服务 和客户端

      检查是否装了SSH包 如果现实有openssh-server 说明系统已经安装了ssh 2 如果系统没有安装ssh 那么可以在线安装 yum install openssh-server 3 设置 ...

  9. java arraylist源码记录

    1. ArrayList 实现了RandomAccess接口, RandomAccess接口用于标记是否可以随机访问 2. 继承了AbstractList类, 因此获取了modcount , modc ...

  10. 深入Garbage First垃圾收集器(三)G1中的垃圾收集

    G1 GC在收集暂停的过程中会回收绝大部分堆分区,唯一的例外是多级并发标记期间的清除阶段. 在清除阶段,如果G1遇到仅仅只存放了垃圾的分区,它就会立刻收集这些分区并将它们放回空闲分区列表中,因此这些分 ...