题目链接: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. HDU 1011 Starship Troopers (树dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...

  2. session再次理解

    1.session介绍: session主要用来存储用户的会话所需的信息(用户行为信息),当用户在同一个服务器上实现不同的操作时,session信息会以变量的形式存储在服务器的内存中,保存用户的状态信 ...

  3. Android自己定义实现循环滚轮控件WheelView

    首先呈上效果图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/d ...

  4. 如何使用Medieval CUE Splitter分割ape,合并ape,制作cue

    1 下载并运行这个软件,点击打开CUE文件,然后找到需要打开的CUE文件.   2 软件会立即弹出一个再次要求打开APE文件的对话框.打开之后会发现APE音乐已经被分割成了一小段一小段.   3 点击 ...

  5. Android Developer:Allocation Tracker演示

    这个演示展示了Allocation Tracker工具在Android Studio中的基本使用方法和流程. Allocation Tracker记录了一个app的内存分配,列出全部分配对象,用于分析 ...

  6. Odoo HRMS应用简介

    Odoo HRMS包含行政管理的大部分功能,包含 部门组织架构 员工清册 岗位规划以及招聘管理 用工合同 考勤管理 休假和加班 费用报销 员工考核 绩效.激励.培训成绩 薪资清册     个角色 角色 ...

  7. C#中的 SET ,GET

    C#中get和SET,看来看去还是看不懂,通俗一点解释一下,用了有什么好处,不用会怎么样如果你这样写是没有什么不一样的. private int __Old; public int Old{ get{ ...

  8. [LeetCode][Java] Combinations

    题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...

  9. webstorm 设置IP 访问 手机测试效果

    http://www.cnblogs.com/gulei/p/5126383.html 前端开发中,经常需要将做好的页面给其他同事预览或手机测试,之前一直用的第三方本地服务器usbwebserver, ...

  10. 汇率换算自然语言理解功能JAVA DEMO

    >>>>>>>>>>>>>>>>>>>>>>>> 欢迎转 ...