Codeforces Round #374 (Div. 2) C DAG上dp
3 seconds
256 megabytes
standard input
standard output
Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1to 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.
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.
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.
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.
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.
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.
Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.
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
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
4
1 2 4 6
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
3
1 3 5
题意 求n个点 m条边 t时间内 从点1到点n经过地点最多的路径解析 dfs搜深度会超时 看了下题解 要用dp写 dp[i][j]表示从i点到n点经过j个点的最少时间
状态转移方程就是dp[i][j]=min(dp[k][j-1]+dis[i][k])k属于i能到达的相邻的点集,用个数组保存下路径。 AC代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 5e3+;
const int inf = 0x3f3f3f3f;
const double epx = 1e-;
const double pi = acos(-1.0);
const ll INF = 1e18;
const ll mod = ;
int n,m,t;
vector<int> mp[maxn];
vector<int> dis[maxn];
int vis[maxn],order[maxn][maxn],dp[maxn][maxn];
void dfs(int c)
{
vis[c]=;
if(c==n)
return;
for(int i=;i<mp[c].size();i++)
{
int k=mp[c][i];
int w=dis[c][i];
if(!vis[k])
dfs(k);
for(int j=;j<=n;j++)
{
if(dp[c][j]>dp[k][j-]+w)
{
dp[c][j]=dp[k][j-]+w;
order[c][j]=k;
}
}
}
}
int main()
{
cin>>n>>m>>t;
mem(vis,);
mem(dp,inf);
mem(order,);
for(int i=;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
mp[u].push_back(v);
dis[u].push_back(w);
}
dp[n][]=;
dfs();
int temp;
for(int i=n;i>=;i--)
{
if(dp[][i]<=t)
{
cout<<i<<endl;
temp=i;
break;
}
}
cout<<<<" ";
int nextt=order[][temp];
while(nextt!=n)
{
cout<<nextt<<" ";
nextt=order[nextt][--temp];
}
cout<<n<<endl;
}
Codeforces Round #374 (Div. 2) C DAG上dp的更多相关文章
- 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)
C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #374 (Div. 2) C(DAG上的DP)
C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #374 (Div. 2)
A题和B题是一如既往的签到题. C题是一道拓扑序dp题,题意是给定一个DAG,问你从1号点走到n号点,在长度不超过T的情况下,要求经过的点数最多,换个思维,设dp[i][j]表示到i号点时经过j个点的 ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
- Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列
A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...
- 拓扑序+dp Codeforces Round #374 (Div. 2) C
http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
随机推荐
- php防止页面刷新代码
//代理IP直接退出 empty($_SERVER['HTTP_VIA']) or exit('Access Denied'); //防止快速刷新 session_start(); $seconds ...
- 对gridview绑定数据的操作方法及自定义显示内容
GridView中Eval和 Bind 的使用 Eval:绑定的是只读数据的显示:Bind:可以绑定只读数据也可以绑定更新数据,Bind方法还把字段和控件的绑定属性联系起来,使得 数据控件(比如Gri ...
- centOS linux 下PHP编译安装详解
一.下载PHP源码包 wget http://php.net/distributions/php-5.6.3.tar.gz 二.添加依赖应用 yum install -y gcc gcc-c++ ...
- C# 处理年月日提取时间
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- MySQL系列(一)--数据类型
如何选择优化的数据类型: 1.通常更小的更好 相同级别的数据类型,选择占据空间更小的数据类型.更小的数据类型通常更快,因为占用更少的磁盘.内存和CPU缓存,处理时需要的 CPU周期也更少,但是要确保需 ...
- Dynamic Web Module版本对应tomcat版本
MyEclipse2017+JDK 1.8环境下 Dynamic Web Module版本3.1要对应tomcat7.0以上版本,不然部署项目时会出现错误(会出现无法部署项目的情况). Dynamic ...
- nginx配置实现负载均衡
一.负载均衡的作用 1.转发功能 按照一定的算法[权重.轮询],将客户端请求转发到不同应用服务器上,减轻单个服务器压力,提高系统并发量. 2.故障移除 通过心跳检测的方式,判断应用服务器当前是否可以正 ...
- 【VScode】使用VScode 来写markdown时序图
准备工作 在VScode中下载插件Markdown Preview Enhanced插件 创建一个.md文件 在VScode中打开文件,界面内点击右键可以看到Open preview to the s ...
- python note of decorator
def decorate_log(decorate_arg,*args,**kwargs): # 存放装饰器参数 def decorate_wrapper(func,*args,**kwargs): ...
- Android ListView setEmptyView
http://my.eoe.cn/yaming/archive/879.html 1 当我们使用ListView或GridView的时候,当列表为空的时候,我们需要一个特殊的View来提示用户操作,于 ...