• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【Codeforces】CodeforcesRound#374(Div.2)--C.Journey(DP)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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.

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 t了,BFS MLE了。看来最优解应该是DP没跑了。

  下面尝试定义转移方程: dp[i][j]表示在经过了i个城市之后到达编号为j的城市所消耗的最小时间。

  转移方程写得好,一切问题就变的简单了。反观这题,题中一共三个变量,经历城市数(希望最大化),消耗的时间(希望最小化),当前城市编号(希望到达n)。

  为了达到上述目的,我们只需要在dp[i][n]<=T中找最大的i就好了, 另外开辟一个parent[i][j]:表示经历了i 个点后从parent[i][j]到达了j点。

IF u can reach v
    dp[i][v] = min(dp[i][v], dp[i - 1][u] + w(u, v));

  最后就是转移方程的第一维度可以从1->n枚举,第二维我们可以通过遍历整个边集合,来找到每一个u->v来更新当前经历过i个点下的dp[i][v]。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>

using namespace std;

const int Maxn = 5005;

int cntE, head[Maxn];
int n, m, T;

typedef struct Node{
    int from, to, next, w;
}Edge;
Edge edges[Maxn];

void init()
{
    memset(head, -1, sizeof head);
    cntE = 0;
}
void addEdge(int u, int v, int w)
{
    edges[cntE].from = u;
    edges[cntE].to = v;
    edges[cntE].w = w;
    edges[cntE].next = head[u];
    head[u] = cntE ++;
}
int parent[Maxn][Maxn], dp[Maxn][Maxn];

void printPath(int pos)
{
    printf("%d\n", pos);
    int id = n;
    vector<int>ans;
    ans.push_back(id);
    for(int i = pos; i > 1 ; i --){
        ans.push_back(parent[i][id]);
        id = parent[i][id];
    }
    printf("%d",ans[ans.size() - 1]);
    for(int i = ans.size() - 2; i >= 0; i --){
        printf(" %d",ans[i]);
    }cout<<endl;
}
void solveByDp()
{
    for(int i = 0; i < n + 1; i ++){
        for(int j = 0; j < n + 1; j ++){
            dp[i][j] = T + 1;
        }
    }
    dp[1][1] = 0;
    //经历了i个点
    int u, v, w, pos = 1;
    for(int i = 2; i <= n; i ++){
        //到达j点
        for(int j = 0; j < m; j ++){
            u = edges[j].from, v = edges[j].to, w = edges[j].w;
            if(dp[i - 1][u] + w < dp[i][v]){
                dp[i][v] = dp[i - 1][u] + w;
                parent[i][v] = u;
            }
        }
        if(dp[i][n] <= T){
            pos = i;
        }
    }
    printPath(pos);
}

int main()
{
    init();
    int u, v, w;
    cin>>n>>m>>T;
    for(int i = 0; i < m; i ++){
        scanf("%d%d%d", &u, &v, &w);
        addEdge(u, v, w);
    }
    solveByDp();
    return 0;
}

 

  


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
用C++实现:删除数组零元素发布时间:2022-07-13
下一篇:
Objective-CAutoreleasePool的实现原理(转)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap