codeforces 721C. Journey 最长路记录路径
来源:互联网 发布:mac ps笔刷怎么安装 编辑:程序博客网 时间:2024/05/18 03:55
Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from1 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 nocyclic 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 exceedingT. 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 131 2 52 3 72 4 8
31 2 4
6 6 71 2 21 3 33 6 32 4 24 6 26 5 1
41 2 4 6
5 5 61 3 33 5 31 2 22 4 34 5 2
31 3 5
题意:有向无环图,n个点,m条有向边,每条边权值t表示经过这条边用时为t,总时间T。从1出发,到n,问最多能经过多少个点,并输出路径,要求总时间<=T。
我的解法:考虑的是spfa,由一个点更新临近点的条件有两个,因为要优先考虑点数,第一个就是经过这个点的的个数,走到这个点能经过更多的点数就更新点数和时间。第二个条件是走到这个点的点数和当前点被更新的点数一样,使得后面有更多的时间去走更多的点,如果被更新过来时间更少,就对当前的的时间进行更新。
用一个二维数组记录路径,第一维是点编号,第二维这个点由起点经过来的时候经过了几个点,保存的是由哪个点更新过来的。
#include <bits/stdc++.h>using namespace std;typedef pair<int,int> pi;typedef pair<int,pi> pii;const int N = 5010;const int INF = 1e9+7;struct node{ int v,w,next; node(){} node(int v,int w,int next): v(v),w(w),next(next){}}E[N];int n,m,T,top;int a[N]; ///路径int num[N]; ///经过点数int dis[N]; ///经过距离(时间)int head[N]; ///头结点int pre[N][N]; ///第一维是点编号,第二维是点数。void Init(){ top = 0; for(int i = 0;i < N;i++){ head[i] = -1; num[i] = 0; dis[i] = INF; for(int j = 0;j < N;j++){ pre[i][j] = 0; } }}void add(int u,int v,int w){ E[top] = node(v,w,head[u]); head[u] = top++;}void spfa(){ num[1] = 1; dis[1] = 0; queue<pii> q; ///pii每个点依次是,点编号,经过的点的数量,经过的距离 q.push(pii(1,pi(1,0))); while(!q.empty()){ pii now = q.front(); q.pop(); int u = now.first; int cnt = now.second.first+1; for(int i = head[u];i != -1;i = E[i].next){ int v = E[i].v; int dist = now.second.second+E[i].w; if(dist > T) continue; if(cnt > num[v]){ num[v] = cnt; dis[v] = dist; pre[v][cnt] = u; q.push(pii(v,pi(cnt,dist))); } else if(cnt == num[v] && dist < dis[v]){ dis[v] = dist; pre[v][cnt] = u; q.push(pii(v,pi(cnt,dist))); } } }}int main(void){ Init(); scanf("%d%d%d",&n,&m,&T); for(int i = 1;i <= m;i++){ int u,v,w; scanf("%d%d%d",&u,&v,&w); add(u,v,w); } spfa(); int pos; ///记录路径 for(int i = n;i >= 1;i--){ if(pre[n][i]){ pos = i; break; } } int cnt = 0; a[cnt++] = n; while(pos > 1){ a[cnt++] = pre[n][pos]; n = pre[n][pos]; pos--; } printf("%d\n",cnt); for(int i = cnt-1;i >= 0;i--) printf("%d ",a[i]);}
- codeforces 721C. Journey 最长路记录路径
- codeforces-C. Journey(广搜+记录路径)
- Codeforces 721C. Journey
- 【10.58%】【codeforces 721C】Journey
- CodeForces 721C Journey(DP)
- codeforces 721C. Journey(dp+拓扑)
- codeforces 839 C Journey
- Codeforces 839C Journey
- Codeforces 839C-Journey
- codeforces 839C Div428B Journey
- 【Codeforces 839 C. Journey】& dfs
- Codeforces 721C. Journey(DAG上的DP)
- codeforces 721C journey(动态规划+拓扑排序)
- Codeforces 721C Journey (简单dp,dfs)
- Codeforces-721C:Journey(最短路上的DP)
- Codeforces Round #374 (Div. 2) C. Journey
- Codeforces Round #374 (Div. 2) C. Journey
- Codeforces Round #374 (Div. 2)C. Journey
- 理解jvm的ClassLoader分析-基础篇
- HDU5923——Prediction(数据结构,并查集)
- 微信video标签全屏无法退出bug
- js引擎总结笔记
- 互联网公司面试问题总结之java基础知识
- codeforces 721C. Journey 最长路记录路径
- GD库学习
- poj 2157 Maze(迷宫)(BFS)
- 互联网公司面试问题总结之数据结构和算法
- 第一章: Activity的生命周期和启动模式:
- 20道经典Java面试基础题
- Linux常用命令
- Android开发在路上:少去踩坑,多走捷径
- HDU-2830-Matrix Swapping II(DP)