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 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 , 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 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 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 ( ≤ n ≤ ,   ≤ m ≤ ,   ≤ T ≤ ) — 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 integers ui, vi, ti ( ≤ ui, vi ≤ n, ui ≠ vi,  ≤ ti ≤ ), 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 ( ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 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 output input output input output

  一道图论题,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 - ][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 = ; int cntE, head[Maxn];
int n, m, T; typedef struct Node{
int from, to, next, w;
}Edge;
Edge edges[Maxn]; void init()
{
memset(head, -, sizeof head);
cntE = ;
}
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 > ; i --){
ans.push_back(parent[i][id]);
id = parent[i][id];
}
printf("%d",ans[ans.size() - ]);
for(int i = ans.size() - ; i >= ; i --){
printf(" %d",ans[i]);
}cout<<endl;
}
void solveByDp()
{
for(int i = ; i < n + ; i ++){
for(int j = ; j < n + ; j ++){
dp[i][j] = T + ;
}
}
dp[][] = ;
//经历了i个点
int u, v, w, pos = ;
for(int i = ; i <= n; i ++){
//到达j点
for(int j = ; j < m; j ++){
u = edges[j].from, v = edges[j].to, w = edges[j].w;
if(dp[i - ][u] + w < dp[i][v]){
dp[i][v] = dp[i - ][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 = ; i < m; i ++){
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
}
solveByDp();
return ;
}

  

【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)的更多相关文章

  1. Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)

    Greenhouse Effect time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  2. Codeforces Round #119 (Div. 2) Cut Ribbon(DP)

    Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. Codeforces Round #658 (Div. 2) D. Unmerge(dp)

    题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...

  4. Codeforces Round #652 (Div. 2) D. TediousLee(dp)

    题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...

  5. Codeforces Round #247 (Div. 2) C. k-Tree (dp)

    题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...

  6. Codeforces Round #260 (Div. 2)C. Boredom(dp)

    C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  7. Codeforces Round #471 (Div. 2) F. Heaps(dp)

    题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...

  8. 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)

    题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...

  9. 【BZOJ】1617: [Usaco2008 Mar]River Crossing渡河问题(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1617 裸dp,很好做. 设f[i]表示i头牛到对岸所需最小时间.sum[i]表示运i头牛到对岸的时间 ...

随机推荐

  1. bootstrap中chosen控件样式有时会冲突

    加上这句话试试 $(".chosen-container").css("width","100%"); 或者 100%改成 100px试试

  2. Bullet:Python的函数中参数是引用吗?

    别的语言中关于函数有传值和传引用的区分. 关于此,流传很广的一个说法是 他们在现象的区别之一就是值传递后的变化,受到影响的就是引用,未受到影响的就是传值.   在学习中,也曾碰到过这个问题,网上关于这 ...

  3. vue-router的基本使用和配置

    1.在main.js文件中引入相关模块以及组件及实例化vue对象配置选项路由及渲染App组件 默认设置如下: import Vue from 'vue' import App from './App' ...

  4. Python 爬虫之第一次接触

    爬豆瓣网电影TOP250名单 ------- 代码未写完,等待更新 import requests from requests.exceptions import RequestException i ...

  5. 【BestCoder Round #93 1001】MG loves gold

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=6019 [题意] 每次选择一段连续的段,使得这一段里面没有重复的元素; 问你最少选多少次; [题解] ...

  6. 认识GIT之入门

    前言 GIT是非常优秀的源代码版本管理工具,经过几年的发展,已经变得非常成熟以及流行,不同于其他的源代码管理系统,值得使用.GIT官网下载在线安装包,经常会中途退出,很有可能的原因是被墙了,所以建议使 ...

  7. [转]十五天精通WCF——第十一天 如何对wcf进行全程监控

    说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...

  8. 2014 北京 DevFest 大会能够报名啦,小伙伴们还在等什么

    一年一度的大型开发人员活动,2014 北京 DevFest 大会站点正式上线: http://devfest.gdgbeijing.org/. 还等什么,開始报名了! 今年 DevFest 大会将再次 ...

  9. Android之——AsyncTask和Handler对照

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46952835 AsyncTask和Handler对照 1 ) AsyncTask实 ...

  10. 使用汇编分析c代码的内存分布

    arm平台下使用反汇编分析c内存分布: arm:使用arm-linux-objdump命令将编译完毕之后的elf文件,进行反汇编. 之后重定向到tmp.s文件里. 第一步变量例如以下c文件. vim ...