【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头牛到对岸的时间 ...
随机推荐
- tp定时任务,传参问题
<?phpnamespace app\command; use think\console\Command;use think\console\Input;use think\console\i ...
- 第二节:numpy之数组切片、数据类型转换、随机数组
- nyoj_366_D的小L_201403011600
D的小L 时间限制:4000 ms | 内存限制:65535 KB 难度:2 描述 一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给 ...
- gap lock/next-key lock浅析Basic-Paxos协议日志同步应用
http://www.cnblogs.com/renolei/p/4673842.html 当InnoDB在判断行锁是否冲突的时候, 除了最基本的IS/IX/S/X锁的冲突判断意外, InnoDB还将 ...
- tomcat理解
- AngularJS:添加检查密码输入是否一致的功能
感谢作者(http://blog.brunoscopelliti.com/angularjs-directive-to-check-that-passwords-match) 利用AngularJS的 ...
- MVC.Net: jqueryval错误
当使用Mvc.net创建Create表单后,firebug Create页面会出现404 Not Found - http://192.168.3.95:7001/bundles/jqueryval& ...
- 2015 测试赛 大神和小伙伴 hihoCoder
立方和公式和平方和公式.表示从来不记得这些公式... 每库礼物不同数量相同,总数=1+2+...+n=(n+1)*n/2 选取礼物的可能性的最大值为[(n+1)*n/2]^3 选取礼物价值重复两次的总 ...
- ubuntu消除登录痕迹
清除登陆系统成功的记录 [root@localhost root]# echo > /var/log/wtmp //此文件默认打开时乱码,可查到ip等信息 [root@localhost roo ...
- java 基础编程day1
public class TextCharType{ public static void(Sting[] args){ char c1 = 'a'; char c2 = '了'; System.ou ...