链接:戳这里
C. Journey
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 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.
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.
Input
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.
Output
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.
Examples
input
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6
input
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5
题意:
给出n个点m条边,每条边都为单向边,权值为w
要求满足从1-n的路径权值和不超过T,且遍历最多的节点的方案,任意输出满足条件的一组
思路:
第一遍bfs将节点入度为0的不为1的节点删掉,这样可以使得只能从1到n且只有节点1入读为0
接下来设置dp[i][j] 表示从1-i经过了j个节点的路径权值
暴力dp统计出答案
bfs从n往回走找出一条符合的路径即可
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include <fstream>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
const int INF=0x3f3f3f3f;
using namespace std;
int n,m;
int T;
struct edge{
int u,v,next;
int w;
}e[1000100];
int head[1000100],tot=0;
void Add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
int deep[1000100];
queue<int > qu;
int dp[5050][5050]; // 当前节点为i,从1-i途径了j个节点的路径权值
void BFS(int x){
while(!qu.empty()) qu.pop();
for(int i=1;i<=n;i++){
for(int j=0;j<=n;j++){
dp[i][j]=2e9+10;
}
}
qu.push(x);
dp[1][0]=0;
while(!qu.empty()){
int u=qu.front();
qu.pop();
for(int i=head[u];i!=-1;i=e[i].next){
int v=e[i].v;
int w=e[i].w;
for(int j=1;j<=n;j++){
if(dp[u][j-1]+w<=T && dp[u][j-1]+w<dp[v][j] && dp[u][j-1]+w>=0){
dp[v][j]=dp[u][j-1]+w;
}
}
deep[v]--;
if(deep[v]==0) qu.push(v);
}
}
}
edge e2[2000100];
int head2[100100],tot2=0;
void Add2(int u,int v,int w){
e2[tot2].v=v;
e2[tot2].w=w;
e2[tot2].next=head2[u];
head2[u]=tot2++;
}
int anw[1000100];
void BFS2(){
int num=0;
for(int i=1;i<=n;i++){
if(dp[n][i]<=T) num=i;
}
int ans=num;
printf("%d\n",num+1);
int u=n;
while(u!=1){
for(int i=head2[u];i!=-1;i=e2[i].next){
int v=e2[i].v;
int w=e2[i].w;
//printf("v=%d w=%d %d %d\n",v,w,dp[v][num-1],dp[u][num]);
if(dp[v][num-1]+w==dp[u][num]){
anw[num]=v;
u=v;
num--;
break;
}
}
}
for(int i=1;i<=ans;i++) cout<<anw[i]<<" ";
cout<<n<<endl;
}
int main(){
mst(head,-1);
mst(head2,-1);
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);
Add2(v,u,w);
deep[v]++;
}
for(int i=1;i<=n;i++) {
if(deep[i]==0) qu.push(i);
}
while(!qu.empty()){
int now=qu.front();qu.pop();
if(now==1) continue;
for(int i=head[now];i!=-1;i=e[i].next){
int v=e[i].v;
deep[v]--;
if(deep[v]==0 && v!=1){
qu.push(v);
}
}
}
//for(int i=1;i<=n;i++) cout<<deep[i]<<" ";cout<<endl;
BFS(1);
/*for(int i=1;i<=n;i++){
for(int j=0;j<=n;j++){
printf("%d %d %d\n",i,j,dp[i][j]);
}
printf("\n");
}*/
BFS2();
return 0;
}
/*
4 3 8
1 4 8
3 4 8
2 4 8
*/