题目描述

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. spring加载属性(properties)文件

    一.注解方式加载 jdbc.driver=org.mariadb.jdbc.Driver jdbc.url=jdbc:mariadb://localhost:3306/kt jdbc.user=roo ...

  2. Centos7单网卡带VLAN多IP配置

    1.需要使用到vconfig软件,首先yum安装vconfig: 使用指令yum install vconfig:(若是本机找不到vconfig安装包,可以通过其他centos7安装yum-utils ...

  3. 逍遥云天 微信小程序开发之获取用户手机号码——使用简单php接口demo进行加密数据解密

    后边要做一个微信小程序,并要能获取用户微信绑定的手机号码.而小程序开发文档上边提供的获取手机号码的接口(getPhoneNumber())返回的是密文,需要服务器端进行解密,但是官方提供的开发文档一如 ...

  4. js 变速动画函数

    //获取任意一个元素的任意一个属性的当前的值---当前属性的位置值 function getStyle(element, attr) { return window.getComputedStyle ...

  5. collections.namedtuple()命名序列元素

    ## collections.namedtuple()命名序列元素 from collections import namedtuple Student = namedtuple("Stud ...

  6. php源码建博客4--实现MVC结构微型框架

    主要: 常量优化路径 自动加载类 优化入口文件 安全访问项目目录 --------------文件结构:-------------------------------------- blog├─App ...

  7. Apache Spark on K8s的安全性和性能优化

    前言 Apache Spark是目前最为流行的大数据计算框架,与Hadoop相比,它是替换MapReduce组件的不二选择,越来越多的企业正在从传统的MapReduce作业调度迁移到Spark上来,S ...

  8. 10种简单的Java性能优化

    你是否正打算优化hashCode()方法?是否想要绕开正则表达式?Lukas Eder介绍了很多简单方便的性能优化小贴士以及扩展程序性能的技巧. 最近“全网域(Web Scale)”一词被炒得火热,人 ...

  9. django创建第一个django项目-2

    安装django 虚拟环境下执行命令: pip install django==1.11.11 查看是否安装成功 pip list 列表中有django说明安装成功 创建工程 命令行移动到想要创建项目 ...

  10. 前端css之float浮动

    浮动的准则,先找前一个块标签,在确认有否清除浮动的条件或者是距离的情况下,如果这一行能摆得下,就继续紧贴前一个标签 如果摆不下,就会另起一行 浮动只有左边和右边 如果是块标签,设置浮动,先把displ ...