题目描述

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. 自定义属性之LinearLayout ImageView TextView模拟图片文字按钮

    一.资源文件: 1.文字选择器: <?xml version="1.0" encoding="utf-8"?> <selector xmlns ...

  2. Subnet Routing Examples

    Routing Table Each row in routing table contains: Destination IP address IP address of next-hop rout ...

  3. Python 学习笔记(九)Python元组和字典(三)

    字典常用方法 copy() 返回一个字典的浅复制 示例:浅拷贝d.copy() 深拷贝引入import copy   copy.deepcopy() >>> help(dict.co ...

  4. Notes 20180308 : 语句

    在讲解流程控制语句之前,我们先来说一下语句的问题.Java中的语句分为声明和赋值语句,条件和循环语句,调用和返回语句:我们之所以每两个放在一起是有深意的,我们大致将语句分为这三块,并以此为纲来说一下, ...

  5. React Native开发中自动打包脚本

    React Native开发中自动打包脚本 在日常的RN开发中,我们避免不了需要将我们编写的代码编译成安装包,然后生成二维码,供需要测试的人员扫描下载.但是对于非原生的开发人员来说,可能不知如何使用X ...

  6. iOS 获取蜂窝网络信号强度 包含iPhoneX XS XR XSMASX (最新)

    1.虽然各种直接获取信号强度的api都被封杀了.但是还有一个另类的黑魔法可以获取到.那就是遍历UIStatusBar了 网络上有的文章写的会崩溃 比如: - (int)getSignalStrengt ...

  7. c和c++单链表

    c++版 #include<iostream> #include<malloc.h> using namespace std; struct node{ int data; n ...

  8. Ruby中的类

    初识ruby中的类 只需要简单的两行 class Point end 如果我们此时实例化一个类那么他会有一些自省(introspection)的方法 p = Point.new p.methodes( ...

  9. DE4加DVI子板实现静态图片显示

    20170906录一下 内容后续补充

  10. DevExpress通过girdcontrol实现分页

    简介:DevExpress中如何实现GridControl的分页功能, 主要是利用DataNavigator和GridControl组合,自定义事件实现分页功能 接下来,我们就去实现分页功能,先看下效 ...