zoukankan      html  css  js  c++  java
  • 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 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
    思路:dfs+dp;
    dp[i][j]表示到i点经过j个点所要的最小花费,然后找dp[n][j]【0《j<=n】的值小于T的最大j,然后在dfs中dp,记录路径;
      1 #include<stdio.h>
      2 #include<algorithm>
      3 #include<stdlib.h>
      4 #include<queue>
      5 #include<iostream>
      6 #include<string.h>
      7 #include<math.h>
      8 #include<set>
      9 #include<map>
     10 #include<vector>
     11 using namespace std;
     12 typedef long long LL;
     13 typedef struct node
     14 {
     15     int  to;
     16     int  cost;
     17 } ss;
     18 vector<ss>vec[6000];
     19 int id[6000];
     20 int ans[6000];
     21 int maxx = 0;
     22 typedef struct pp
     23 {
     24     short int x;
     25     short int y;
     26     pp()
     27     {
     28         x = -1;
     29         y = -1;
     30     }
     31 } ad;
     32 int dp[5006][5006];
     33 ad roa[5006][5006];
     34 void dfs(int n,int m,int c);
     35 int main(void)
     36 {
     37     int n,m,k;
     38     scanf("%d %d %d",&n,&m,&k);
     39     {
     40         for(int i = 0; i <= 5000; i++)
     41         {
     42             for(int j = 0; j <= 5000; j++)
     43             {
     44                 dp[i][j] = 1e9+1;
     45             }
     46         }
     47         while(m--)
     48         {
     49             ss ak;
     50             int x,y,cost;
     51             scanf("%d %d %d",&x,&y,&cost);
     52             ak.to = y;
     53             ak.cost = cost;
     54             vec[x].push_back(ak);
     55         }
     56         dp[1][1] = 0;
     57         dfs(1,n,1);
     58         int maxx = 0;
     59         for(int i = 0; i <= n; i++)
     60         {
     61             if(dp[n][i] <= k)
     62             {
     63                 maxx = i;
     64             }
     65         }
     66         printf("%d
    ",maxx);
     67         int cn = 0;
     68         int u = roa[n][maxx].x;
     69         int xx = roa[n][maxx].x;
     70         int yy =roa[n][maxx].y;
     71         ans[cn++] = n;
     72         while(true)
     73         {
     74             ans[cn++] = xx;
     75             if(xx==1)break;
     76             int cc = roa[xx][yy].y;
     77             int bb = roa[xx][yy].x;
     78             yy = cc;
     79             xx = bb;
     80         }
     81         printf("1");
     82         for(int i = cn-2; i >= 0; i--)
     83         {
     84             printf(" %d",ans[i]);
     85         }
     86     }
     87     return 0;
     88 }
     89 void dfs(int n,int m,int c)
     90 {
     91     if(n == m)return ;
     92     for(int i = 0; i < vec[n].size(); i++)
     93     {
     94         ss ak = vec[n][i];
     95         if(dp[ak.to][c+1] > (LL)dp[n][c]+(LL)ak.cost)
     96         {
     97             dp[ak.to][c+1] = dp[n][c]+ak.cost;
     98             roa[ak.to][c+1].x = n;
     99             roa[ak.to][c+1].y = c;
    100             dfs(ak.to,m,c+1);
    101         }
    102     }
    103 }
    
    
    油!油!you@
  • 相关阅读:
    java工程中如何连接redis数据库?
    linux启动达梦数据库
    Maven项目无法编译resources文件夹下资源
    java 后端定义的大写字段传到前端后变成小写
    springboot同时接收表单数据和文件
    java:Fastjson将object转为json时"$ref"的相关问题
    consul注册中心搭建
    maven-compiler-plugin 插件详解
    org.activiti.api.runtime.shared.UnprocessableEntityException
    https配置
  • 原文地址:https://www.cnblogs.com/zzuli2sjy/p/5925678.html
Copyright © 2011-2022 走看看