CF721C. Journey[DP DAG]
3 seconds
256 megabytes
standard input
standard 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 nocyclic 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 Ttime units passing it.
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.
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.
4 3 13
1 2 5
2 3 7
2 4 8
3
1 2 4
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
4
1 2 4 6
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
3
1 3 5
题意:单向,没有回路,没有重边自环,限制时间,求1到n最多经过几个点,并输出这些点任意方案
因为没有环,又保证1和n连通,一开始想树形DP,并不好做,然后发现这是有向边
突然发现,这不就是有向无环图,有向无环图DAG的最短路最长路可以用DP来做,扩展一下应该也可以
f[i][j]表示从i到n经过j个点的时间 PS:因为忘判vis TLE一次
//
// main.cpp
// c
//
// Created by Candy on 9/30/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
const int N=,M=,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,T,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
}
int f[N][N],vis[N];
void dp(int u){
if(u==n) return;
int child=;
if(vis[u]) return;
vis[u]=;
for(int i=h[u];i;i=e[i].ne){
child++;
int v=e[i].v,w=e[i].w;
dp(v);
for(int j=;j<=n;j++) if(f[v][j-]<INF)
f[u][j]=min(f[u][j],f[v][j-]+w);
}
}
void print(int u,int d){
printf("%d ",u);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(f[v][d-]<INF&&f[u][d]==f[v][d-]+w) {print(v,d-);break;}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();T=read();
for(int i=;i<=m;i++){
u=read();v=read();w=read();
ins(u,v,w);
}
memset(f,,sizeof(f));
f[n][]=;
dp();
int num=;
for(int i=n;i>=;i--)
if(f[][i]<=T) {num=i;break;}
printf("%d\n",num);
print(,num);
return ;
}
CF721C. Journey[DP DAG]的更多相关文章
- 拓扑排序+DP CF721C Journey
CF721C Journey 给出一个\(n\)个点\(m\)条边的有向无环图. 问从\(1\)到\(n\),在距离不超过\(k\)的情况下最多经过多少点,并输出一个方案. \(topo\)+\(DP ...
- NYOJ16|嵌套矩形|DP|DAG模型|记忆化搜索
矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...
- 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)
「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...
- CF #374 (Div. 2) C. Journey dp
1.CF #374 (Div. 2) C. Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...
- codeforces 721C C. Journey(dp)
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforce 721C DP+DAG拓扑序
题意 在一个DAG上,从顶点1走到顶点n,路径上需要消费时间,求在限定时间内从1到n经过城市最多的一条路径 我的做法和题解差不多,不过最近可能看primer看多了,写得比较复杂和结构化 自己做了一些小 ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
- VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]
传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...
随机推荐
- javascript --- 原型初探七日谈(一)
在javascript中,像原型,闭包这样的概念,只要我们能领悟其中的原理,一切都会显得格外清晰与明了. 原型属性(prototype): 下面我们简单定义一个函数 function her(a, b ...
- Flex Viewer
一.Flex Viewer简介 Flex Viewer是ESRI公司推出的可以高效开发基于WEB的地理信息应用系统的一种完全免费的应用程序框架.业务人员使用该框架可以无需任何额外的编程就能够通过简单配 ...
- Arcgis创建SDE_Geometry、SDO_Geometry的区别【转】
1. SDO_GEOMETRY Oracle Spatial在MDSYS模式下定义了一系列几何类型.函数来支持空间数据的存储和使用,最为人耳熟能详的就是SDO_GEOMETRY这种类型——当然,Arc ...
- AVAudioSession初探
根据文档,AudioSession规定了app和系统音频行为交互的规范,一个app只有一个AudioSession的单例. app通过设置自己AudioSession的单例的属性来告诉系统自身想达到的 ...
- 深入.net(多态一)
代码优化技术: 当您在 编写一个类时,如果您发现你需要编写的“属性”和“方法”曾经在已有的类中实现,则,您可以将其共用的“属性”和“方法”剪切到一个新的“类”中,然后,让两个类共同继承这个“新类”.( ...
- 自己用js写的日历(在考勤中使用,显示员工的日期的考勤情况)
1.HTML部分 <div id="AttendanceDataDetailDiv"> <div class="A_close"> &l ...
- 使用jqgrid的C#/asp.net mvc开发者的福音 jqgrid-asp.net-mvc
你是否使用jqgrid? 你是否想在C#/asp.net mvc中使用jqgrid? 那你很可能曾经为了分析jqgrid的request url用fiddler忙活了2个小时.(如果你要使用jqgri ...
- fillStyle线性渐变
废话小说,沾待马 <!DOCTYPE HTML> <head> <meta charset = "utf-8"> <title>ca ...
- ORACLE中伪表 dual 的用法
dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录.我们可以用它来做很多事情,如下: 1.查看当前用户,可以在 SQL Plus中执行下面语句 sel ...
- 每日Scrum(9)
今天我们小组进行了软件的测试和界面的美化,特别是在主界面美化方面下了一些功夫,找了很多图片,把格式也处理的很完美,符合界面的一个框架,看起来,美观多了,至此,软件的beta版是基本完成了.