CodeForces Round#374 C:Journey(dfs图论+dp)

博客内容涉及CodeForces的一道题目,讨论了如何帮助Irina在限定时间内从第1个景点到达第n个景点并访问尽可能多的景点。题目保证了从第1个景点到第n个景点存在一条不超过T时间单位的路线。输入包括景点数量、道路数量和Irina的停留时间,以及每条道路的起始点、终点和时间消耗。解决方案通过直接建图并使用动态规划(DP)进行状态转移,求解最大可达景点数量及路径。
摘要由CSDN通过智能技术生成


原题链接:点击打开链接

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 from1 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 nocyclic 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 exceedingT. 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时间内最多能走几个节点,并输出相应节点。

直接建图跑dp

dp[i][j]表示在第i个节点时已经走过j个节点数时所花最小时间。

状态转移方程:dp[i][j]=min(dp[k][j-1]+dis[k][i],dp[i][j])

#include <cstdio>  
#include <cstring>  
#include <cmath>  
#include <cstdlib>  
#include <algorithm>  
#include <queue>    
#include <map>  
#include <vector>  
#include <iostream>
#define go(a,b) for(int i=a;i<=b;++i)
#define INF 0x3f3f3f3f  
#define mst(a, b) memset(a, (b), sizeof(a))  
#define MOD 100000007  
#define ll long long  
#define lson node<<1, l, mid  
#define rson node<<1|1, mid+1, r  
const  int maxn = 5005;
using namespace std;
int edgenum ;

struct Edge {
	int to,cap,next;
}edge[maxn];
int n, m, t;
int fa[maxn][maxn];
int dp[maxn][maxn];
int head[maxn];
void init() {
	edgenum = 0;
	mst(head, -1);
}
void addEdge(int u,int v,int w) {
	edge[edgenum].cap = w;
	edge[edgenum].to = v;
	edge[edgenum].next = head[u];
	head[u] = edgenum;
	edgenum++;
}
void dfs(int u,int step)
{
	for (int i = head[u]; i != -1; i = edge[i].next) {
		int v = edge[i].to;
		int dist = edge[i].cap;
		if (dp[v][step + 1] > dp[u][step] + dist && dp[u][step] + dist <= t) {
			dp[v][step + 1] = dp[u][step] + dist;
			fa[v][step + 1] = u;
			dfs(v, step + 1);
		}
	}
}
void print(int u,int i) {
	if (u == 1) {
		printf("1 ");
		return;
	}
	else {
		print(fa[u][i], i - 1);
	}
	printf("%d ", u);
	if (u == n)
		printf("\n");

}
int main() {
	init();
	scanf("%d%d%d", &n, &m, &t);
	for (int i = 1; i <= m; ++i) {
		int u, v, d;
		scanf("%d%d%d", &u, &v, &d);
		addEdge(u, v, d);
	}
	for (int i = 1; i <= n + 1; i++) {
		for (int j = 1; j <= n + 1; j++) {
			dp[i][j] = INF;
		}
	}

	dp[1][1] = 0;
	dfs(1, 1);
	for (int i = n; i >= 1; i--) {
		if (dp[n][i] <= t) {
			cout << i << endl;
			print(n, i);
			return 0;
		}
	}
	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值