codeforces 721 Journey (拓扑序+DP)

73 篇文章 0 订阅
5 篇文章 0 订阅


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 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个景点,要求你在T时间之内,从1景点到n景点旅游经过的景点数尽量多,输出T时间内1到n经过的景点数,并输出路劲。

解题思路:如果直接dfs会直接TLE,那么我们考虑取点的时候是有序的,即用拓扑优化,之后用dp【i】【j】= cnt,即当前在i景点,经过j个景点所花费最短的时间。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int N = 5000+100;
int dp[N][N],index[N],pre[N][N];
const int inf = 0x3f3f3f3f;
vector<pair<int,int> >E[N];
stack<int>st;
queue<int>que;
int main()
{
	ios::sync_with_stdio(false);
	int n,m,T,i,j,cnt,num;
	int u,v,w;
	cin>>n>>m>>T;
	for(i=1;i<=m;i++) {
		cin>>u>>v>>w;
		if(u!=n && w<=T) {
			index[v]++;// 记录每个端点的入度 
			E[u].push_back(make_pair(v,w)); //用链表存图!!! 
		}
	}
	pre[1][1]=-1;
	for(i=1;i<=n;i++) {
		if(!index[i]) que.push(i);
	}
	memset(dp,inf,sizeof(dp));
	dp[1][1]=0;
    while(!que.empty()) {          //拓扑序保证当前点为最优的,可以节省时间,优化dfs 
    	u=que.front();
    	que.pop();
    	for(i=0;i<E[u].size();i++) {
    		v=E[u][i].first;
    		w=E[u][i].second;
    		index[v]--;
    		 
    		if(index[v]==0) que.push(v);
    		for(j=1;j<=n;j++) {         // 进行dp,   dp[i][j]=cost  i表示当前的走到的景点,j表示总共走过了景点的数量,cost表示最少花费的时间 
    			if(dp[v][j+1]>dp[u][j]+w && dp[u][j]+w <= T) {   //这边要注意一下,总花费如果大于T,就不行跟新 
    				dp[v][j+1]=dp[u][j]+w;
    	     		pre[v][j+1]=u;
				}
			}
		}
	}
	for(i=n;i>=2;i--) {
		if(dp[n][i]<=T) break;
	}
	cnt=n;num=i;
	while(cnt!=-1) {
		st.push(cnt);
		cnt=pre[cnt][num--];
	}
	cout<<i<<endl;
	while(!st.empty()) {
		cout<<st.top()<<" ";
		st.pop();
	}
	return 0;
}






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值