[CodeForce721C]Journey

Posted chen574118090

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[CodeForce721C]Journey相关的知识,希望对你有一定的参考价值。

题目描述

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的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 721C journey

Codeforces 721C [dp][拓扑排序]

CodeForces - 721C 拓扑排序+dp

[Codeforces 839C] Journey

POJ1935 Journey(树形dp)

CodeForces 721B Journey (DP)