zoukankan      html  css  js  c++  java
  • Codeforces Round #374 (Div. 2) C DAG上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 1to 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
    题意 求n个点 m条边 t时间内 从点1到点n经过地点最多的路径解析 dfs搜深度会超时 看了下题解 要用dp写 dp[i][j]表示从i点到n点经过j个点的最少时间
    状态转移方程就是dp[i][j]=min(dp[k][j-1]+dis[i][k])k属于i能到达的相邻的点集,用个数组保存下路径。

    AC代码
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <string.h>
     4 #include <stdlib.h>
     5 #include <iostream>
     6 #include <sstream>
     7 #include <algorithm>
     8 #include <string>
     9 #include <queue>
    10 #include <map>
    11 #include <vector>
    12 #include <iomanip>
    13 #define mem(a,b) memset(a,b,sizeof(a))
    14 using namespace std;
    15 typedef long long ll;
    16 const int maxn = 5e3+50;
    17 const int inf = 0x3f3f3f3f;
    18 const double epx = 1e-10;
    19 const double pi = acos(-1.0);
    20 const ll INF = 1e18;
    21 const ll mod = 998244353;
    22 int n,m,t;
    23 vector<int> mp[maxn];
    24 vector<int> dis[maxn];
    25 int vis[maxn],order[maxn][maxn],dp[maxn][maxn];
    26 void dfs(int c)
    27 {
    28     vis[c]=1;
    29     if(c==n)
    30         return;
    31     for(int i=0;i<mp[c].size();i++)
    32     {
    33         int k=mp[c][i];
    34         int w=dis[c][i];
    35         if(!vis[k])
    36             dfs(k);
    37         for(int j=2;j<=n;j++)
    38         {
    39             if(dp[c][j]>dp[k][j-1]+w)
    40             {
    41                  dp[c][j]=dp[k][j-1]+w;
    42                  order[c][j]=k;
    43             }
    44         }
    45     }
    46 }
    47 int main()
    48 {
    49     cin>>n>>m>>t;
    50     mem(vis,0);
    51     mem(dp,inf);
    52     mem(order,0);
    53     for(int i=0;i<m;i++)
    54     {
    55         int u,v,w;
    56         cin>>u>>v>>w;
    57         mp[u].push_back(v);
    58         dis[u].push_back(w);
    59     }
    60     dp[n][1]=0;
    61     dfs(1);
    62     int temp;
    63     for(int i=n;i>=1;i--)
    64     {
    65         if(dp[1][i]<=t)
    66         {
    67             cout<<i<<endl;
    68             temp=i;
    69             break;
    70         }
    71     }
    72     cout<<1<<" ";
    73     int nextt=order[1][temp];
    74     while(nextt!=n)
    75     {
    76         cout<<nextt<<" ";
    77         nextt=order[nextt][--temp];
    78     }
    79     cout<<n<<endl;
    80 }
    View Code
  • 相关阅读:
    怎样用c/c++编程连接mysql数据库?
    c#中转义符总结
    制作文件系统出错运行mkubimagemlc2
    面试中常考的C函数库中的字符串处理函数
    移动支付模式再添新军:指纹支付
    面试中常考的单链表处理
    选购冰箱勿被概念迷惑:并非功能越多越好
    地址转换
    JavaScriptGarden/
    配置非安装版的mysql 5.5
  • 原文地址:https://www.cnblogs.com/stranger-/p/8596423.html
Copyright © 2011-2022 走看看