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

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的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值