codeforces 721C. Journey 最长路记录路径

本文介绍了一种使用SPFA算法解决有向无环图中从起点到终点最多能经过多少个点的问题,同时给出了具体的实现代码及示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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表示经过这条边用时为t,总时间T。从1出发,到n,问最多能经过多少个点,并输出路径,要求总时间<=T。

我的解法:考虑的是spfa,由一个点更新临近点的条件有两个,因为要优先考虑点数,第一个就是经过这个点的的个数,走到这个点能经过更多的点数就更新点数和时间。第二个条件是走到这个点的点数和当前点被更新的点数一样,使得后面有更多的时间去走更多的点,如果被更新过来时间更少,就对当前的的时间进行更新。

用一个二维数组记录路径,第一维是点编号,第二维这个点由起点经过来的时候经过了几个点,保存的是由哪个点更新过来的。

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
typedef pair<int,pi> pii;
const int N = 5010;
const int INF = 1e9+7;
struct node{
    int v,w,next;
    node(){}
    node(int v,int w,int next):
        v(v),w(w),next(next){}
}E[N];
int n,m,T,top;
int a[N];       ///路径
int num[N];     ///经过点数
int dis[N];     ///经过距离(时间)
int head[N];    ///头结点
int pre[N][N];  ///第一维是点编号,第二维是点数。

void Init()
{
    top = 0;
    for(int i = 0;i < N;i++){
        head[i] = -1;
        num[i] = 0;
        dis[i] = INF;
        for(int j = 0;j < N;j++){
            pre[i][j] = 0;
        }
    }
}

void add(int u,int v,int w)
{
    E[top] = node(v,w,head[u]);
    head[u] = top++;
}

void spfa()
{
    num[1] = 1;
    dis[1] = 0;
    queue<pii> q;    ///pii每个点依次是,点编号,经过的点的数量,经过的距离
    q.push(pii(1,pi(1,0)));
    while(!q.empty()){
        pii now = q.front();
        q.pop();
        int u = now.first;
        int cnt = now.second.first+1;
        for(int i = head[u];i != -1;i = E[i].next){
            int v = E[i].v;
            int dist = now.second.second+E[i].w;
            if(dist > T) continue;
            if(cnt > num[v]){
                num[v] = cnt;
                dis[v] = dist;
                pre[v][cnt] = u;
                q.push(pii(v,pi(cnt,dist)));
            }
            else if(cnt == num[v] && dist < dis[v]){
                dis[v] = dist;
                pre[v][cnt] = u;
                q.push(pii(v,pi(cnt,dist)));
            }
        }
    }
}

int main(void)
{
    Init();
    scanf("%d%d%d",&n,&m,&T);
    for(int i = 1;i <= m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
    }
    spfa();
    int pos;   ///记录路径
    for(int i = n;i >= 1;i--){
        if(pre[n][i]){
            pos = i;
            break;
        }
    }
    int cnt = 0;
    a[cnt++] = n;
    while(pos > 1){
        a[cnt++] = pre[n][pos];
        n = pre[n][pos];
        pos--;
    }
    printf("%d\n",cnt);
    for(int i = cnt-1;i >= 0;i--)
        printf("%d ",a[i]);
}


### Codeforces Div.2 比赛难度介绍 Codeforces Div.2 比赛主要面向的是具有基础编程技能到中级水平的选手。这类比赛通常吸引了大量来自全球不同背景的参赛者,包括大学生、高中生以及一些专业人士。 #### 参加资格 为了参加 Div.2 比赛,选手的评级应不超过 2099 分[^1]。这意味着该级别的竞赛适合那些已经掌握了一定算法知识并能熟练运用至少一种编程语言的人群参与挑战。 #### 题目设置 每场 Div.2 比赛一般会提供五至七道题目,在某些特殊情况下可能会更多或更少。这些题目按照预计解决难度递增排列: - **简单题(A, B 类型)**: 主要测试基本的数据结构操作和常见算法的应用能力;例如数组处理、字符串匹配等。 - **中等偏难题(C, D 类型)**: 开始涉及较为复杂的逻辑推理能力和特定领域内的高级技巧;比如图论中的最短路径计算或是动态规划入门应用实例。 - **高难度题(E及以上类型)**: 对于这些问题,则更加侧重考察深入理解复杂概念的能力,并能够灵活组合多种方法来解决问题;这往往需要较强的创造力与丰富的实践经验支持。 对于新手来说,建议先专注于理解和练习前几类较容易的问题,随着经验积累和技术提升再逐步尝试更高层次的任务。 ```cpp // 示例代码展示如何判断一个数是否为偶数 #include <iostream> using namespace std; bool is_even(int num){ return num % 2 == 0; } int main(){ int number = 4; // 测试数据 if(is_even(number)){ cout << "The given number is even."; }else{ cout << "The given number is odd."; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值