$dp$,拓扑排序。

记$dp[i][j]$表示走到节点$i$,走过了$j$个点的最小时间,然后就可以递推了。要注意的是节点$1$的入度一开始不一定等于$0$。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
} const int INF=0x7fffffff;
const int maxn=;
int dp[maxn][maxn];
struct Edge
{
int v,nx; int w;
}e[maxn];
int h[maxn],sz,r[maxn];
int n,m,T;
queue<int>Q;
int pre[maxn][maxn]; void add(int u,int v,LL w)
{
e[sz].v=v; e[sz].w=w;
e[sz].nx=h[u]; h[u]=sz++;
} int main()
{
scanf("%d%d%d",&n,&m,&T); for(int i=;i<=n;i++)
{
h[i]=-;
for(int j=;j<=n;j++)
{
dp[i][j]=INF;
pre[i][j]=-;
}
} for(int i=;i<=m;i++)
{
int u,v; LL w; scanf("%d%d%d",&u,&v,&w);
add(u,v,w); r[v]++;
} dp[][]=;
for(int i=;i<=n;i++) if(r[i]==) Q.push(i);
bool flag=;
while(!Q.empty())
{
int top=Q.front(); Q.pop();
if(top==) flag=;
if(flag==)
{
for(int i=h[top];i!=-;i=e[i].nx)
{
int to=e[i].v;
r[to]--;
if(r[to]==) Q.push(to);
}
continue;
}
for(int i=h[top];i!=-;i=e[i].nx)
{
int to=e[i].v;
for(int j=;j<=n;j++)
{
if(dp[top][j-]==INF) continue;
if(dp[top][j-]+e[i].w>T) continue;
if(dp[top][j-]+e[i].w>=dp[to][j]) continue; pre[to][j]=(top-)*n+j--;
dp[to][j]=dp[top][j-]+e[i].w;
}
r[to]--;
if(r[to]==) Q.push(to);
}
} int sum;
for(int i=;i<=n;i++) if(dp[n][i]<=T) sum=i; cout<<sum<<endl;
int nowx=n,nowy=sum; stack<int>S;
while()
{
S.push(nowx);
int tx,ty;
tx=pre[nowx][nowy]/n; tx++;
ty=pre[nowx][nowy]%n; ty++;
if(pre[nowx][nowy]==-) break;
nowx=tx; nowy=ty;
}
while(!S.empty())
{
cout<<S.top()<<" "; S.pop();
} return ;
}

CodeForces 721C Journey的更多相关文章

  1. CodeForces 721C Journey(拓扑排序+DP)

    <题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...

  2. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  3. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  4. 【10.58%】【codeforces 721C】Journey

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. Codeforces 839C Journey【DFS】

    C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...

  6. Codeforces 839C Journey - 树形动态规划 - 数学期望

    There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can r ...

  7. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  8. [Codeforces 839C] Journey

    [题目链接] http://codeforces.com/contest/839/problem/C [算法] 概率DP 时间复杂度 : O(N) [代码] #include<bits/stdc ...

  9. CodeForces 788B--Weird journey

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Little ...

随机推荐

  1. SaaS模式和实现思路

    EFW框架开发的系统支持SaaS模式和实现思路 回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dAD ...

  2. Hibernate在自由状态和持久的状态转变

    在Hibernate在.一PO术后可能长时间,session过时关闭.此时PO它一直是游离状态的对象,在这种状态下,以被转换成持久战,有几种方法如下: 1.session.saveOrUpdate(o ...

  3. Java(5/6和8)中interface和Abstract Class

    这篇文章主要是自己在使用java的过程中对自己一些之前常困惑的问题的一些总结. 正如题目所言,这篇博客主要是讨论java中的接口与抽象类的区别,有自己的使用心得,以及自己平时在使用的过程中遇到的问题及 ...

  4. Gitlab,Github与Bitbucket

    这段时间开始做毕设,决定使用git来管理代码和相关的文档. 同时希望有一个远程托管,决定在github.bitbucket,以及我自己搭建的gitlab服务器中间选一个,最终决定使用bitbuckt. ...

  5. IOS开发小功能2:二维码扫描界面的设计(横线上下移动)

    效果图如上,实现的是一个二维码扫描界面. 下面我贴出线条上下移动的代码,至于二维码的代码是用的第三方库. 首先是整体的结构: 注意下面的库文件一个都不能少,否则会报错. TLTiltHighlight ...

  6. C/C++基础知识总结——多态性

    1. 多态性的概述 1.1 多态是指同样的消息被不同类型的对象接收时导致不同的行为.所谓消息是指对垒的成员函数的调用,不同行为是指不同的实现. 1.2 多态的实现 (1) 实现角度讲多态可分为两类:编 ...

  7. [转]iOS: About diagnostic capabilities

    Source:http://support.apple.com/kb/HT6331 Each of these diagnostic capabilities requires the user to ...

  8. C#做的颜色工具

    常常会用到绘制以及配色,每次看到 framework 里边的 KnowColor.xxx 我就一阵......到底啥颜色啊,干脆做一个一劳永逸的工具吧.功能包含: 可以实现枚举所有系统颜色 圆形.矩形 ...

  9. 在html中使用javascript显示本地图片的

    <html> <head> <script type="text/javascript"> function getFullPath(obj){ ...

  10. linux serial 登录 cubieboard

    折腾半天linux下的putty,最后搞得实在没办法,放弃putty改用minicom 1. 先安装minicom sudo apt-get install minicom 2.配置com minic ...