Journey

题目链接:http://codeforces.com/problemset/problem/721/C

dp/记忆化搜索/拓扑排序

刚开始想到用bfs+dp,fst(然而中间有一步逻辑错了,不得不说pretest真心弱,然后rank一掉回到解放前),改正后MLE了,状态没去重存不下...

看了下其他人的,基本用的是记忆化搜索,复杂度是O(m*n);看了下队友ac的,以为是bfs,问了下回答说是拓扑排序(不会=、=)

游少给了一种直接dp的思路Orz,复杂度是O(m*n):

其中time[i][j]表示经过i个结点到达j结点所需要的时间,

pre[i][j]表示经过i个结点到达j结点前的结点序号。

代码如下:

 #include<cstdio>
#include<stack>
#define N 5005
using namespace std;
struct node{
int u,v,t;
}egde[N];
int time[N][N],pre[N][N];
int n,m,T;
int main(void){
scanf("%d%d%d",&n,&m,&T);
for(int i=;i<=m;++i)
scanf("%d%d%d",&egde[i].u,&egde[i].v,&egde[i].t);
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
time[i][j]=T+;
time[][]=;
for(int i=;i<n;++i)
for(int j=;j<=m;++j){
int u=egde[j].u;
int v=egde[j].v;
int t=egde[j].t;
if(time[i][u]+t<time[i+][v]){
time[i+][v]=time[i][u]+t;
pre[i+][v]=u;
}
}
int floor;
for(int i=n;i>=;--i)
if(time[i][n]<T+){
floor=i;
printf("%d\n",floor);
break;
}
int nod=n;
stack<int>s;
while(pre[floor][nod]){
s.push(pre[floor][nod]);
nod=pre[floor][nod];
floor--;
}
while(!s.empty()){
nod=s.top();
s.pop();
printf("%d ",nod);
}
printf("%d\n",n);
}

Journey的更多相关文章

  1. CF721C. Journey[DP DAG]

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  2. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  3. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  4. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  5. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  6. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  7. HDOJ-三部曲一(搜索、数学)- A Knight's Journey

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  8. 【推公式】UVa 10995 - Educational Journey

    1A~,但后来看人家的代码好像又写臭了,T^T... Problem A: Educational journey The University of Calgary team qualified f ...

  9. poj 3544 Journey with Pigs

    Journey with Pigs Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3004   Accepted: 922 ...

  10. HDU 5477 A Sweet Journey 水题

    A Sweet Journey Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. LeetCode题解 15题 第二篇

    之前写过一篇,这是第二篇.上一篇用了多种编程语言来做,这一次是以学算法为主,所以打算都用python来完成. 4. Median of Two Sorted Arrays There are two ...

  2. HTML <dl> 标签

    <html><body><h2>一个定义列表:</h2><dl>   <dt>计算机</dt>   <dd&g ...

  3. CodeForces 384C Milking cows

    水题. 对于两个$0$,肯定是先删去后面的$0$,再删去前面的$0$. 对于两个$1$,肯定是先删去前面的$1$,再删去后面的$1$. 对于一个$0$和一个$1$,无论先删哪一个,对答案做出的贡献都是 ...

  4. http服务 Web API的使用

    http服务 Web API的使用 一.概念: Web API是网络应用程序接口. 详情百度百科: http://baike.baidu.com/link?url=X1l2dlU9FlQmupX24- ...

  5. USACO 3.4 Raucous Rockers

    Raucous Rockers You just inherited the rights to N (1 <= N <= 20) previously unreleased songs ...

  6. [HMLY]11.MVVM架构

    概要 MVC架构,Model-View-Controller,如图一所示为一个典型的MVC设置. 图一:mvc Model呈现数据 View呈现用户界面 Controller调节两者之间的交互.从Mo ...

  7. maven插件报错之解决

    maven插件报错之解决 用m2eclipse创建Maven项目时报错 maveneclipsebuilddependenciesauthorizationplugins 用m2eclipse创建 ...

  8. 替换ubuntu 14.04的源

    1. 背景(为什么要替换)安装ubuntu,默认源是(http://extras.ubuntu.com/ubuntu),国内访问很慢...当我们用apt-get安装软件包或者更新时有时很慢,所以才想到 ...

  9. 在VMware上安装CentOS -7步骤详解

    在VMware上安装CentOS -7 一.下载好VMware虚拟机 二.准备好CentOS的镜像文件 在这里安装之前博主都已准备好了. 废话就少啰嗦啦!现在开始安装步骤了 1.首先打开VMware创 ...

  10. Python学习笔记——基础篇【第六周】——Subprocess模块

    执行系统命令 可以执行shell命令的相关模块和函数有: os.system os.spawn* os.popen*          --废弃 popen2.*           --废弃 com ...