题目描述

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 no cyclic routes between showplaces.

最近,伊琳娜来到了柏林最著名的城市之一——柏林托夫市。这个城市有n个展厅,编号从1到N,其中一些是通过单向道路连接的。在贝拉托夫的道路设计的方式,使没有循环路线之间的展示。

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.

伊琳娜起初站在1号展览馆,她的旅程的终点是N号展览馆。自然,伊琳娜希望在旅途中尽可能多地参观展览馆。然而,伊琳娜在贝拉托夫的停留是有限的,她不能在那里超过t个时间单位。

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 T time units passing it.

帮助伊琳娜在不超过t的时间内确定从1号展厅到N号展厅的旅程中可以参观多少个展厅。保证至少有一条从1号展厅到N号展厅的路线,这样伊琳娜将花费不超过t个时间单位通过。

输入格式

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.

输入的第一行包含三个整数n、m和t(2≤n≤5000、1≤m≤5000、1≤t≤109)-展示场所的数量、它们之间的道路数量以及Irina在Berlatov的停留时间。

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.

下一条M线描述了贝拉托夫的道路。其中i-th包含3个整数ui,vi,ti(1≤ui,vi≤n,ui≠vi,1≤109),这意味着有一条从ShowPlace ui开始通向ShowPlace vi的路,而Irina花费ti时间单位通过它。保证道路不形成循环路线。

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.

输出单个整数k(2≤k≤n)-在第一行时间内,从1号展厅到N号展厅,Irina可以参观的最大展厅数量。

Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

在第二行中输出k个不同的整数——按照遇到的顺序,显示伊琳娜将要去的地方的索引。

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

题解

网上的方法大都是使用动态规划来做,事实上这个题可以不用dp。如图



我们设f[i]表示到达第i个点时最多可以经过的点数,再设cnt[i]表示点i在当前状态下的入度大小,再以入度的大小从小到大遍历每一个点以更新它能走过的最大点数。因为每次更新之后最近的点的入度一定是1,因此我们一定可以在之前就得到到达该点的最优情况。

    queue<int> q;
q.push(1);
while(!q.empty()){
u=q.front();
q.pop();
for(register int i=p[u];~i;i=E[i].next){
v=E[i].v;
cnt[v]--;
if(cnt[v]==1)q.push(v);
if(dis[u]+w<=T){
if(dis[v]>dis[u]+w)dis[v]=dis[u]+w;
}
}
}

当然,这样会产生一个问题:如果之前得到的最大点数不是最优的,我们就无法更新出更优的情况。因此,我们再用一个二维数组[box]来记录当前可能的较优情况。当然,不可能将所有出现过的情况全部记录下来。事实上,对于每个点都有固定的初始状态数量。我们在更新任意一个初始状态时,就用更新之后的状态代替初始状态。

if(dis[u][i]+w<=T){
dis[v][i]=dis[u][i]+w;
}

代码如下

#include<bits/stdc++.h>
#define maxn 10000
#define maxm 10000
using namespace std;
inline char get(){
static char buf[3000],*p1=buf,*p2=buf;
return p1==p2 && (p2=(p1=buf)+fread(buf,1,3000,stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
register char c=get();register int f=1,_=0;
while(c>'9' || c<'0')f=(c=='-')?-1:1,c=get();
while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=get();
return _*f;
}
struct edge{
int u,v,w,next;
}E[maxm];
int p[maxn],eid;
inline void init(){
for(register int i=1;i<maxn;i++)p[i]=-1;
eid=0;
}
inline void insert(int u,int v,int w){
E[eid].u=u;
E[eid].v=v;
E[eid].w=w;
E[eid].next=p[u];
p[u]=eid++;
}
int n,m,k;
int dis[maxn][100];
int main(){
init();
n=read();m=read();k=read();
for(register int i=1;i<=n;i++){
int u=read(),v=read(),w=read();
insert(u,v,w);
}
for(register int i=1;i<maxn;i++){
for(register int j=1;j<100;j++)dis[i][j]=0x3f3f3f3f;
}
queue<int> q;
q.push(1);
while(!q.empty()){
u=q.front();
q.pop();
for(register int i=p[u];~i;i=E[i].next){
v=E[i].v;
cnt[v]--;
if(cnt[v]==1)q.push(v);
if(dis[u][i]+w<=T){
dis[v][i]=dis[u][i]+w;
}
}
}
cout<<dis[n];
return 0;
}

[CodeForce721C]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 ...

随机推荐

  1. 使用@Aspect切面进行让JDBC自动关闭(Spring AOP)

    原生jdbc进行测试 demo:https://github.com/weibanggang/springjdbcAspect.git

  2. php auto_prepend_file和auto_append_file的妙用

    这是一个鲜为人知的设置! auto_prepend_file 和 auto_append_file 是在php.ini中进行配置的2个参数,auto_prepend_file 表示在php程序加载第一 ...

  3. iOS之利用runtime,避免可变数组和可变字典为nil或者数组越界导致的崩溃

    NSArray.NSMutableArray.NSDictionary.NSMutableDictionary.是我们的在iOS开发中非常常用的类.当然,在享受这些类的便利的同时,它们也给我们带来一些 ...

  4. ABAP术语-Technical Object

    Technical Object 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/18/1111205.html Generic term f ...

  5. drawImage画本地资源,在真机无法显示

    把图片的路径改成本地的绝对路径

  6. Java实例 Part1:Java基础输出语句

    ** Part1:Java基础输出语句 ** 第一部分最基础,就是标准的输出语句. ps:(目前还没熟悉这个编辑器,先尝试一下) Example01 : 输出"hello world&quo ...

  7. STM32(9)——通用定时器作为输入捕捉

    通用定时器作为输入捕获的使用.我们将用 TIM5 的通道 1 (PA0)来做输入捕获,捕获 PA0 上高电平的脉宽(用 WK_UP 按键输入高电平),通过串口打印高电平脉宽时间 输入捕获简介 输入捕获 ...

  8. python应用:爬虫框架Scrapy系统学习第三篇——初识scrapy

    scrapy的最通用的爬虫流程:UR2IM U:URL R2:Request 以及 Response I:Item M:More URL 在scrapy shell中打开服务器一个网页 cmd中执行: ...

  9. rails应用中各数据平台的对接

    1.mongo #Gemfile添加如下两个gem包gem 'mongoid', '~> 5.1.0' gem 'mongo', '~> 2.4’ @client = Mongo::Cli ...

  10. python学习笔记~INI、REG文件读取函数(自动修复)

    引入configparser,直接read整个INI文件,再调用get即可.但需要注意的是,如果INI文件本身不太规范,就会报各种错,而这又常常不可避免的.本文自定义函数通过try...except. ...