【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头牛到对岸的时间 ...
随机推荐
- yum插件
参考文章: http://www.linuxfly.org/post/297/ [root@dnstest07.tbc /home/ahao.mah] #rpm -qa |grep yum yum-p ...
- json 添加 和删除两种方法
<script> var test = { name: "name", age: "12" }; var countrys = { "ne ...
- Django Template(模板系统)
一.Django模板 内置模板标签和过滤器 二.常用操作 两种特殊符号: {{ }} 和 {% %} 变量相关的用: {{ }} 逻辑相关的用: {% %} 2.1 变量 在Django的模 ...
- Django的基础教程
学Django需要什么基础? 1. Django是 python 语言写的一个Web框架包,所以你得知道一些 Python 基础知识. 2. 其次你最好有一些做网站的经验,懂一些网页 HTML, CS ...
- Django——7 常用的查询 常用的模型字段类型 Field的常用参数 表关系的实现
Django 常用的查询 常用的查询方法 常用的查询条件 常用字段映射关系 Field常用参数 表关系的实现 查用的查询方法 这是需要用到的数据 from django.http import Htt ...
- bupt summer training for 16 #5 ——数据结构
https://vjudge.net/contest/173780 A.假设 Pt = i,则由Ppi = i得 Ppt = t = Pi 所以就有 if Pt = i then Pi = t #in ...
- 嵌入式linux和嵌入式android系统有什么区别和联系?
转自:http://bbs.eeworld.com.cn/thread-430437-1-1.html 这个问题很多人问,尤其是初入嵌入式的菜鸟.其实大家都认为android是java,已经不是lin ...
- nyoj_33_蛇形填数_201308221636
蛇形填数时间限制:3000 ms | 内存限制:65535 KB 难度:3描述 在n*n方陈里填入1,2,...,n*n,要求填成蛇形.例如n=4时方陈为:10 11 12 19 16 13 28 ...
- iOS:去除UITableView的空白行
要去除UITableView在运行时显示的多余空白行,只需要将TableView的Style从Plain改为Grouped即可.
- [Vue] Code split by route in VueJS
In this lesson I show how to use webpack to code split based on route in VueJS. Code splitting is a ...