题目链接: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. 快速上手 Echarts

    最近使用到了 百度的 Echarts 数据可视化工具,这里简单介绍如何快速上手. 一.下载 这里选择目前最新版本,4.2.1 地址:https://github.com/apache/incubato ...

  2. hough变换检测直线和圆

    图像测量和机器视觉作业: 提取图像中的直线和点的位置坐标,将其按一定顺序编码存入一文本文件,并在原图像上叠加显示出来. 下午实验了一下: 程序环境:vs2013(活动平台为x64)+opencv3.1 ...

  3. procomm plus

    procomm plus这是查看串口数据的软件.

  4. Qt跨平台的一个例程

    我的同事penk在近期北京的Hackathon展示了一个在多平台的例程. 非常多开发人员对这个挺感兴趣的. 今天我就把这个资源介绍给大家. 这是同一个用Qt写的应用.能够同一时候在Ubuntu Des ...

  5. linux系列之-—04 自动删除n天前日志【转】

    让Linux系统定时清理一些不需要的文件,日志很有必要 1. 删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; ...

  6. 《C程序猿:从校园到职场》出版预告(4):从“散兵游勇”到“正规部队”

    看过电视剧<楚汉传奇>的朋友应该对这个场景还有印象:当刘邦第一次去找项羽帮忙的时候.他们一行人看到了项羽军营是怎样练兵的.想到自己练兵的方法,当时就震惊了."刘家军"就 ...

  7. EC2的维护更新

     2014年9月28日 近期几天.我们收到了一些客户关于我们即将进行维护更新的问题.下面是AWS全球Blog网站对这个问题的说明,供客户參照. 我们已经開始通知那些受影响的客户,关于我们即将实施的 ...

  8. kubernetes资源调度之LimitRange

    系列目录 LimitRange从字面意义上来看就是对范围进行限制,实际上是对cpu和内存资源使用范围的限制 前面我们讲到过资源配额,资源配额是对整个名称空间的资源的总限制,是从整体上来限制的,而Lim ...

  9. java arraylist源码记录

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

  10. 04 http协议模拟登陆发帖

    <?php require('./http.class.php'); $http = new Http('http://home.verycd.com/cp.php?ac=pm&op=s ...