Codeforces Round #374 (Div. 2)-C. Journey DP

C. Journey

题意:

在一个DAG(有向无环图)中,问从1 到 n 点,在时间限制K下,最多能游玩几个地点,把游玩的顺序顺便输出。

思路:

感觉dp,一维不够就加一维,我一开始有想到dp,但是只是一维的去推,推着感觉不正确。这次用dp[i][j],表示到j点已游玩i个地点的最少时间。

DAG中一般要想到求拓扑序。即保证dp从左到右。

这题这个人直接两重循环求dp,不知道为啥也是对的 ,确实是有道理,因为dp[ i - 1]这一行的所有情况对于 i 行都是确定的,且i行一定是在i-1行存在的基础上推过来。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <stack>
using namespace std;

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;

#define fi first
#define se second

#define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)


const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf  = 0x3f3f3f3f;
template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}
// #define _DEBUG;         //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------show time----------------*/

            const int maxn = 5009;
            int dp[maxn][maxn];
            int pre[maxn][maxn];
            int n,m,k;
            struct node 
            {
                int u,v,w;
            }e[maxn];
int main(){
            scanf("%d%d%d", &n, &m, &k);

            for(int i=1; i<=m; i++){
                int u,v,w;
                scanf("%d%d%d", &u,&v,&w);
                e[i].v = v, e[i].w = w;
                e[i].u = u;
            }
            memset(dp,inf,sizeof(dp));
            dp[1][1] = 0;

            for(int i=2; i<=n; i++){
                for(int j=1; j<=m; j++){
                    int u = e[j].u,w = e[j].w;
                    int v = e[j].v;
                    if(dp[i][v] > dp[i-1][u] + w ){
                        dp[i][v] = dp[i-1][u] + w;
                        pre[i][v] = u;
                    }
                }
            }
            int id = -1;
            for(int i=n; i>=1;i--){

                if(dp[i][n]<=k){
                    id = i;
                    break;
                }
            }
            printf("%d\n",id);
            stack<int>s;
            int o = n;
            s.push(o);
            for(int i=id; i>=2; i--){
                s.push(pre[i][o]);
                o = pre[i][o];
            }
            while(!s.empty()){
                printf("%d ",s.top());
                s.pop();
            }

            return 0;
}
CF 721C

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/9344615.html