CodeForces 721C Journey(DP)

题目链接:http://codeforces.com/problemset/problem/721/C

C. Journey
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

题目解析:用cost[i][j]记录行进到i点并且i点作为第j个行进到的点所用的时间;递归从第n个点往回求出每一个cost的值;用parent[u][i]记录u节点在某一条道路上的父节点。

知识点:图论,STL


AC代码如下:

#include 
using namespace std;

#define INF 0x3f3f3f3f
#define mp make_pair
typedef pair<int, int> ii;
vector<vector > graph;
vector<int> visited;
vector<vector<int> > cost,parent;
int n,m,t;

void dfs(int  u){
    visited[u] = 1;
    for(vector::iterator i = graph[u].begin(); i != graph[u].end(); i++){
        int v = i->first, t = i->second;
        if(!visited[v]) dfs(v);
        for(int i = 1; i <= n; i++){
        //从v到u,从v作为道路上的第一个点开始遍历
            if(cost[v][i - 1] + t < cost[u][i]){
                cost[u][i] = cost[v][i-1] + t;
                parent[u][i] = v;
            }
        }
    }
}
int main(){
    cin >> n >> m >> t;
    graph.resize(n);
    for(int i = 0; i < m; i++){
        int a,b,t; 
        cin >> a >> b >> t;
        graph[--a].push_back(mp(--b,t));
    }
    visited.resize(n);
    cost.resize(n+1,vector<int>(n+1,INF));
    parent.resize(n+1,vector<int>(n+1,INF));
    cost[n-1][0] = 0; //最终节点往回行进0步 
    dfs(0);
    int j = 0;
    //找出最大的从0号点开始,经过的点并且小于给定时间t的点
    for(int i = 0; i <= n; i++){
        if(cost[0][i] <= t) j = i;
    }
    cout << j + 1 << endl;
    int cur = 0;
    while(cur != n-1){
        cout << cur + 1 << " ";
        cur = parent[cur][j--];
    }
    cout << n << endl;
    return 0;
}

你可能感兴趣的:(CodeForces,DP,图论)