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

题目链接: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.枚举长度*枚举起点*枚举终点:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ms(a, b)  memset((a), (b), sizeof(a))
 4 typedef long long LL;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 5000+10;
 9 
10 struct node
11 {
12     int v, w;
13 };
14 
15 int n,m,T;
16 vector<node>G[maxn];
17 int dp[maxn][maxn], fa[maxn][maxn];
18 
19 void init()
20 {
21     scanf("%d%d%d",&n,&m,&T);
22 
23     for(int i = 1; i<=n; i++)
24         G[i].clear();
25 
26     for(int i = 1; i<=m; i++)
27     {
28         int u;
29         node e;
30         scanf("%d%d%d",&u,&e.v,&e.w);
31         G[u].push_back(e);
32     }
33 
34     ms(fa,0);
35     for(int i = 1; i<=n; i++)
36     for(int j = 1; j<=n; j++)
37         dp[i][j] = INF;
38     dp[1][1] = 0;
39 }
40 
41 void prt(int len, int u)
42 {
43     if(len>1)
44         prt(len-1, fa[len][u]);
45 
46     printf("%d ",u);
47 }
48 
49 void solve()
50 {
51     int k;
52     for(int len = 2; len<=n; len++)
53     {
54         for(int u = 1; u<n; u++)
55         {
56             for(int i = 0; i<G[u].size(); i++)
57             {
58                 int v = G[u][i].v;
59                 int w = G[u][i].w;
60 
61                 if(dp[len-1][u]==INF) continue; //少了这步,如果继续用int,会溢出,因为INF+1e9
62 
63                 int tmp = dp[len-1][u] + w;
64                 if(tmp<=T && dp[len][v]>tmp)
65                 {
66                     dp[len][v] = tmp;
67                     fa[len][v] = u;
68                 }
69             }
70         }
71         if(dp[len][n]!=INF)
72             k = len;
73     }
74 
75     printf("%d\n",k);
76     prt(k,n); putchar('\n');
77 
78 }
79 
80 int main()
81 {
82     init();
83     solve();
84     return 0;
85 }
View Code

 

2.枚举长度*枚举边:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ms(a, b)  memset((a), (b), sizeof(a))
 4 typedef long long LL;
 5 const int INF = 2e9;
 6 const LL LNF = 9e18;
 7 const int mod = 1e9+7;
 8 const int maxn = 5000+10;
 9 
10 struct node
11 {
12     int u, v, w;
13     void read()
14     {
15         scanf("%d %d %d",&u, &v, &w);
16     }
17 }edge[maxn];
18 
19 int n,m,T;
20 int dp[maxn][maxn], fa[maxn][maxn];
21 
22 void init()
23 {
24     scanf("%d%d%d",&n,&m,&T);
25     for(int i = 1; i<=m; i++)
26         edge[i].read();
27 
28     ms(fa,0);
29     for(int i = 1; i<=n; i++)
30     for(int j = 1; j<=n; j++)
31         dp[i][j] = INF;
32     dp[1][1] = 0;
33 }
34 
35 void prt(int len, int u)
36 {
37     if(len>1)
38         prt(len-1, fa[len][u]);
39 
40     printf("%d ",u);
41 }
42 
43 void solve()
44 {
45     int k;
46     for(int len = 2; len<=n; len++)
47     {
48         for(int i = 1; i<=m; i++)
49         {
50             int u = edge[i].u;
51             int v = edge[i].v;
52             int w = edge[i].w;
53 
54             if(dp[len-1][u]==INF) continue;  //少了这步,如果继续用int,会溢出,因为INF+1e9
55 
56             int cost = dp[len-1][u] + w;
57             if(cost<=T && cost<dp[len][v])
58             {
59                 dp[len][v] = cost;
60                 fa[len][v] = u;
61             }
62         }
63 
64         if(dp[len][n]!=INF)
65             k = len;
66     }
67 
68     printf("%d\n",k);
69     prt(k,n); putchar('\n');
70 }
71 
72 int main()
73 {
74     init();
75     solve();
76     return 0;
77 }
View Code

 


 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538695.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。在编写C程序时,需要注意变量的声明和定义、指针的使用、内存的分配与释放等问题。C语言中常用的数据结构包括: 1. 数组:一种存储同类型数据的结构,可以进行索引访问和修改。 2. 链表:一种存储不同类型数据的结构,每个节点包含数据和指向下一个节点的指针。 3. 栈:一种后进先出(LIFO)的数据结构,可以通过压入(push)和弹出(pop)操作进行数据的存储和取出。 4. 队列:一种先进先出(FIFO)的数据结构,可以通过入队(enqueue)和出队(dequeue)操作进行数据的存储和取出。 5. 树:一种存储具有父子关系的数据结构,可以通过中序遍历、前序遍历和后序遍历等方式进行数据的访问和修改。 6. 图:一种存储具有节点和边关系的数据结构,可以通过广度优先搜索、深度优先搜索等方式进行数据的访问和修改。 这些数据结构在C语言中都有相应的实现方式,可以应用于各种不同的场景。C语言中的各种数据结构都有其优缺点,下面列举一些常见的数据结构的优缺点: 数组: 优点:访问和修改元素的速度非常快,适用于需要频繁读取和修改数据的场合。 缺点:数组的长度是固定的,不适合存储大小不固定的动态数据,另外数组在内存中是连续分配的,当数组较大时可能会导致内存碎片化。 链表: 优点:可以方便地插入和删除元素,适用于需要频繁插入和删除数据的场合。 缺点:访问和修改元素的速度相对较慢,因为需要遍历链表找到指定的节点。 栈: 优点:后进先出(LIFO)的特性使得栈在处理递归和括号匹配等问题时非常方便。 缺点:栈的空间有限,当数据量较大时可能会导致栈溢出。 队列: 优点:先进先出(FIFO)的特性使得
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值