【codeforces 721C】【DAG 按照拓扑排序记忆化DFS】C. Journey 【DAG图,5000个点,5000条边 让你求从1到n的路径长度不超过T中经过点数最多的一条 】

传送门:C. 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 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 
题意:

DAG图,5000个点,5000条边 

让你求从1到n的路径长度不超过T中经过点数最多的一条

思路:


f[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的最小距离
nxt[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的后继 ,这里用short可以优化不少内存

然后 按照拓扑排序记忆化搜索一下

如果我们有x的后继为y
那么我们肯定可以对y做dfs,在对y做dfs的时候,显然不会走到x(DAG图)
换而言之,y的所有可能的前驱(包括前驱的前驱)都不会影响到dfs(y)的值
于是,这里就保证了该dp的正确拓扑序,也就可以在O(n^2)的复杂度内出解。


代码:

#include <bits/stdc++.h>
#define pr(x) cout << #x << "= " << x << "  " ;
#define pl(x) cout << #x << "= " << x << endl;
#define ll __int64
#define mst(ss,b) memset(ss,b,sizeof(ss));
using  namespace  std;
const int N=5050;
const int inf=0x3f3f3f3f;

int n, m, T;
int f[N][N];//f[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的最小距离
short nxt[N][N];//nxt[i][j]表示当前在点i,接下来经过j个点(包括自己)到n点的后继
vector< pair<int,int> >a[N];
bool vis[N];
void dfs(int x){
  if (vis[x])return; vis[x] = 1;
  for (auto it : a[x]){
    dfs(it.first);
    for (int i = 2; i <= n; ++i){
      int dis = f[it.first][i - 1] + it.second;
      if (dis < f[x][i]){
        f[x][i] = dis;
        nxt[x][i] = it.first;
      }
    }
  }
}

void print(){
  for (int i = n; ; --i)if (f[1][i] <= T){
    printf("%d\n", i);
    int x = 1; printf("%d ", x);
    while (x != n){
      x = nxt[x][i--];
      printf("%d ", x);
    }puts("");
    break;
  }
}

int main(){
  while(~scanf("%d%d%d",&n, &m, &T)){
    for (int i = 1; i <= n; ++i)a[i].clear(), vis[i] = 0;
    for (int i = 1; i <= m; ++i){
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      if (y == 1 || x == n)continue;
      a[x].push_back({ y,z });
    }
    mst(f, inf);
    f[n][1] = 0; vis[n] = 1;
    dfs(1);
    print();
  }
  return 0;
}
    原文作者:拓扑排序
    原文地址: https://blog.csdn.net/guhaiteng/article/details/52718084
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞