题目描述

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. 安全清理Xcode 缓存垃圾

    安全清理Xcode缓存垃圾方法: 经验证,Xcode缓存垃圾存储在~/Library/Developer/Xcode/DerivedData/路径下,缓存和Xcode的版本有关(如同一台Mac安装2个 ...

  2. Ubuntu下配置安装telnet server

    1.安装xinetd 以及telnetd # apt-get install xinetd telnetd 2.配置文件/etc/inetd.conf #vi /etc/inetd.conf # St ...

  3. Nodejs中获取参数以及处理参数

    先看题干效果 在这里我们建了一个表单 填入表单需要提交的信息 对两个参数进行获取和一个加法计算 表单html代码 <form action='http://localhost:8080' met ...

  4. 什么是 better-scroll(转自知乎网 : 黄轶)

    什么是 better-scroll better-scroll 是一个移动端滚动的解决方案,它是基于 iscroll 的重写,它和 iscroll 的主要区别在这里.better-scroll 也很强 ...

  5. 如何优雅的使用C语言绘制一只小猪佩奇

    今天我们来用C语言画一只小猪佩奇---社会.社会....在画小猪佩奇之前,我们先使用带符号的距离长 (signed distance field,SDF) 来画一个圆形. 使用这个方法表示形状,但是这 ...

  6. 竞赛题解 - Ikki's Story IV-Panda's Trick

    Ikki's Story IV-Panda's Trick - 竞赛题解 也算是2-sat学习的一个节点吧 终于能够自己解决一道2-sat的题了 ·题目 一个圆上有n个点按顺时针编号为 0~n-1 , ...

  7. jquery 去除空格

    /** * 是否去除所有空格 * @param str * @param is_global 如果为g或者G去除所有的 * @returns */ function Trim(str,is_globa ...

  8. 理解Redux以及如何在项目中的使用

    今天我们来聊聊Redux,这篇文章是一个进阶的文章,建议大家先对redux的基础有一定的了解,在这里给大家推荐一下阮一峰老师的文章: http://www.ruanyifeng.com/blog/20 ...

  9. JavaScript脚本加载相关知识

    <script>标签的位置 HTML4规范允许<script>可以放在<head>或<body>中. 但是,放在<head>中会导致性能问题 ...

  10. Java性能优化的50个细节

    在JAVA程序中,性能问题的大部分原因并不在于JAVA语言,而是程序本身.养成良好的编码习惯非常重要,能够显著地提升程序性能. 1. 尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时 ...