【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)
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)的更多相关文章
- 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 ...
- 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 ...
- Codeforces Round #658 (Div. 2) D. Unmerge(dp)
题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...
- Codeforces Round #652 (Div. 2) D. TediousLee(dp)
题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...
- Codeforces Round #247 (Div. 2) C. k-Tree (dp)
题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...
- 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 ...
- Codeforces Round #471 (Div. 2) F. Heaps(dp)
题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...
- 【Codeforces】CF 5 C Longest Regular Bracket Sequence(dp)
题目 传送门:QWQ 分析 洛谷题解里有一位大佬讲的很好. 就是先用栈预处理出可以匹配的左右括号在数组中设为1 其他为0 最后求一下最长连续1的数量. 代码 #include <bits/std ...
- 【BZOJ】1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1617 裸dp,很好做. 设f[i]表示i头牛到对岸所需最小时间.sum[i]表示运i头牛到对岸的时间 ...
随机推荐
- js移动端 可移动滑块
document.addEventListener('DOMContentLoaded', function() { //动态创建回到首页dom var backDom = document.crea ...
- 多目标跟踪笔记二:Efficient Algorithms for Finding the K Best Paths Through a Trellis
Abstract 本文提出了一种新的方法来寻找不相交k最优路径.最坏情况下计算复杂度为N3log(N).该方法比WVD算法(https://www.cnblogs.com/walker-lin/p/1 ...
- Xcode 插件因为UUID原因不能使用解决办法
Xcode 经常因为一些原因不能使用,需要重新在 ~/Library/Application Support/Developer/Shared/Xcode/Plug-ins目录下对每一个插件包下的p ...
- P1002 过河卒 【递推、简单动规】
题目描述 棋盘上AA点有一个过河卒,需要走到目标BB点.卒行走的规则:可以向下.或者向右.同时在棋盘上CC点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒 ...
- Scrapy实战:使用IDE工具运行爬虫
一般我们运行爬虫程序都是使用命令行,比如:scrapy crwal sobook.不过这多少有些不方便,可以使用下面的方法使用IDE的方式运行爬虫 我这边使用的是pycharm软件,在pycharm里 ...
- 单表:SQL语句关键字的执行顺序
表和数据: -- 创建表 CREATE TABLE `person` ( `id` ) NOT NULL AUTO_INCREMENT, `name` ) NOT NULL, `age` ) ', ` ...
- 一次vue-cli 2.x项目打包优化经历(优化xlsx插件)
一.分析各模块打包后大小 用vue-cli创建的项目,已经集成 webpack-bundle-analyzer.详见文件 build/webpack.prod.conf.js,代码如下: if (co ...
- java debug jdk(转载)
Debug info unavailable 解决之道 从事Java的小伙伴们估计都有断点代码的习惯,可以很方便的查看运行期代码中一些变量的值. 但是JDK中有些类你会发现是无法断点的,即使你在IDE ...
- 洛谷 P1120 小木棍 [数据加强版]
P1120 小木棍 [数据加强版] 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它 ...
- LeetCode258——Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...