Codeforces Round #374 (Div. 2) C. Journey 拓扑排序+dp

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 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 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 

暴力dfs T了,想了想dp应该可以,先拓扑排序,然后按照顺序dp。

dp[i][j]表示从1到i走j步最多剩余多少time,状态转移方程为dp[v][k+1]=max(dp[v][k+1],dp[u][k]-e.cost),在维护一个pre数组标记路径。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<stack>
const int inf=0x3f3f3f3f;
typedef long long LL;
using namespace std;

const int MAXN=5010;
struct edge
{
    int to,cost;
    edge()
    {

    }
    edge(int t,int c)
    {
        to=t;
        cost=c;
    }
};
vector<edge> G[MAXN];
int pre[MAXN][MAXN];
int dp[MAXN][MAXN];
int Q[MAXN];
bool visit[MAXN];
int indegree[MAXN];
int num;
stack<int> S;
int n,m,T;
int a,b,c;
int max(int l,int r)
{
    return l>r?l:r;
}
void TopoSort()
{
    memset(visit,0,sizeof(visit));
    for(int i=1;i<=n;i++)
    {
        if(!indegree[i]&&!visit[i])
        {
            Q[num++]=i;
        }
    }
    for(int i=0;i<num;i++)
    {
        int u=Q[i];
        for(int i=0;i<G[u].size();i++)
        {
            indegree[G[u][i].to]--;
            if(!indegree[G[u][i].to])
                Q[num++]=G[u][i].to;
        }
    }
}
int main()
{

    while(cin>>n>>m>>T)
    {
        num=0;
       for(int i=0;i<=n;i++)
       {
           G[i].clear();
       }
       memset(indegree,0,sizeof(indegree));
        for(int i=1;i<=m;i++)
        {
            scanf("%d %d %d",&a,&b,&c);
            G[a].push_back(edge(b,c));
            indegree[b]++;
        }
        TopoSort();
//        for(int i=0;i<num;i++)
//        {
//            cout<<Q[i]<<endl;
//        }
        memset(dp,-1,sizeof(dp));
        memset(pre,-1,sizeof(pre));
        dp[1][0]=T;
        int va,u,v;
        for(int i=0;i<num;i++)
        {
            u=Q[i];
            for(int j=0;j<G[u].size();j++)
            {
                v=G[u][j].to;
                va=G[u][j].cost;
                //cout<<u<<" "<<v<<" "<<va<<endl;
                for(int k=0;k<=n;k++)
                {
                    if(dp[u][k]>=va)
                    {
                       // cout<<dp[u][k]<<endl;
                        //cout<<dp[v][k+1]<<endl;
                        if(dp[v][k+1]<dp[u][k]-va)
                        {
                            dp[v][k+1]=dp[u][k]-va;
                            pre[v][k+1]=u;
                        }
                    }
                }
            }
        }
//        for(int i=1;i<=n;i++)
//        {
//            for(int j=0;j<=n;j++)
//            {
//                cout<<dp[i][j]<<" ";
//            }
//            cout<<endl;
//        }
//        for(int i=1;i<=n;i++)
//        {
//            for(int j=0;j<=n;j++)
//            {
//                cout<<pre[i][j]<<" ";
//            }
//            cout<<endl;
//        }
        int MAX;
        for(int i=n;i>=0;i--)
        {
            if(dp[n][i]>=0)
            {
                MAX=i+1;
                break;
            }
        }
        cout<<MAX<<endl;
        int t=MAX-1;
        S.push(n);
        while(pre[n][t]!=-1)
        {
            n=pre[n][t];
            t--;
            S.push(n);
            //cout<<n<<" "<<t<<endl;
        }
        cout<<S.top();
        S.pop();
        while(!S.empty())
        {
            cout<<" "<<S.top();
            S.pop();
        }
        cout<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值