zoukankan      html  css  js  c++  java
  • CodeForces-721C-Journey(DAG, DP)

    链接:

    https://vjudge.net/problem/CodeForces-721C

    题意:

    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.

    思路:

    Dp[i][j] 为i点到n点经过j个点的时间.可以在图上得到方程Dp[i][j] = min(dp[k][j-1]+dis(i,k)).

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int MAXN = 5e3+10;
    
    struct Edge
    {
        int to, dis;
    };
    vector<Edge> G[MAXN];
    bool Vis[MAXN];
    int To[MAXN][MAXN], Dp[MAXN][MAXN];
    int n, m, t;
    
    void Dfs(int x)
    {
        Vis[x] = true;
        if (x == n)
            return;
        for (int i = 0;i < G[x].size();i++)
        {
            int v = G[x][i].to;
            int dis = G[x][i].dis;
            if (!Vis[v])
                Dfs(v);
            for (int j = 2;j <= n;j++)
            {
                if (Dp[v][j-1]+dis < Dp[x][j])
                {
                    Dp[x][j] = Dp[v][j-1]+dis;
                    To[x][j] = v;
                }
            }
        }
    }
    
    int main()
    {
        memset(Dp, 0x3f3f3f3f, sizeof(Dp));
        scanf("%d %d %d
    ", &n, &m, &t);
        int u, v, w;
        for (int i = 1;i <= m;i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            G[u].push_back(Edge{v, w});
        }
        Dp[n][1] = 0;
        Dfs(1);
        int maxp = 1;
        for (int i = n;i >= 1;i--)
        {
            if (Dp[1][i] <= t)
            {
                maxp = i;
                break;
            }
        }
        printf("%d
    ", maxp);
        int p = 1, x = maxp;
        printf("1");
        while (x > 1)
        {
            printf(" %d", To[p][x]);
            p = To[p][x--];
        }
        puts("");
    
        return 0;
    }
    
  • 相关阅读:
    洛谷 P5057 [CQOI2006]简单题 题解
    洛谷 P3368 【模板】树状数组 2 题解
    洛谷 P3374 【模板】树状数组 1 题解
    洛谷 P2023 [AHOI2009]维护序列 题解
    洛谷 P2253 好一个一中腰鼓! 题解
    求最长不下降/上升/下降/不上升子序列
    [SQL Server]Index/deadlock
    Ubuntu 14.04下从源码安装qt4.x
    Ubuntu系统下Import cv2提示no modules ...错误
    Ubuntu 14.04下安装CUDA8.0
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11385440.html
Copyright © 2011-2022 走看看