$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. WebIM(2)---消息缓存

    WebIM系列文章 在一步一步打造WebIM(1)一文中,已经介绍了如何实现一个简单的WebIM,但是,这个WebIM有一个问题,就是每一次添加消息监听器时,都必须访问一次数据库去查询是否有消息,显然 ...

  2. Mysql高级之主从复制

    原文:Mysql高级之主从复制 主从复制不就是多台服务器嘛!,一个改变另一个也改变啦,内容其实都一样! 原理: 对数据库进行操作会生成一个文件,binlog(二进制文件),从服务器配置relaylog ...

  3. Linux Shell 示例精解(第七章 gawk编程)转载

    第七章 gawk功能:gawk编程 7.1.1 数字和字符串常量     初始化和类型强制  在awk中,变量不需要定义就可以直接使用,使用一个变量就是对变量的定义.变量的类型可以试数字.字符串,或者 ...

  4. PHP中生成验证码

    //生成图片$im = imagecreatetruecolor(100,30);//生成颜色,当第一次调用生成颜色的方法,是生成背景颜色,默认是黑色//如果想自定义背景颜色,用到imagefill函 ...

  5. WCF连接被意外关闭

    WCF项目中出现常见错误的解决方法:基础连接已经关闭: 连接被意外关闭 在我们开发WCF项目的时候,常常会碰到一些莫名其妙的错误,有时候如果根据它的错误提示信息,一般很难定位到具体的问题所在,而由于W ...

  6. [置顶] 网页提交方式post和get的区别和联系

    都知道GET一般用于获取/查询资源信息,而POST一般用于更新资源信息.但具体区别呢?可以互换吗? 主要区别有:1传输数据的方式不同,2传输的数据多少不同,3传输数据的安全性不同4,后台获取数据的方式 ...

  7. ToolBox Analysis & Design

    话说小菜过做已近3年,虽出身PHP后项目大多涉及.net,系统也做得比较繁杂,从常见的CMS,企业OA,ERP,也涉及到电商系统的开发定制,爬虫,工具不一而足,其中web系统居多. 由于表现良好,时常 ...

  8. linux 之 snprintf函数用法

    int snprintf(char *restrict buf, size_t n, const char * restrict  format, ...); 函数说明:最多从源串中拷贝n-1个字符到 ...

  9. Jquery文本框值改变事件(支持火狐、ie)

    Jquery值改变事件支持火狐和ie浏览器,并且测试通过,绑定后台代码可以做成autocomplete控件. 具体代码列举如下: $(document).ready(function () { $(& ...

  10. JDBC之事务隔离级别以及ACID特性

    JDBC之事务隔离级别以及ACID特性 事务隔离级别: 1.更新遗失(Lost update) 两个事务都同时更新一行数据,但是第二个事务却中途失败退出,导致对数据的两个修改都失效了.这是因为系统没有 ...