技不如人甘拜下风
这题网上说法有 建反向边和先拓扑
都是为了每个点之前将其前驱都遍历到
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 5005;
const int INF = 0x3f3f3f3f;
int N,M,T;
vector<pair<int,int> > mp[MAXN];
ll dp[MAXN][MAXN];
int vis[MAXN];
int ans;
vector<int> op;
void dfs(int x) {
if(vis[x]) return;
for(int i = 0; i < (int)mp[x].size(); ++i) {
int y = mp[x][i].first;
if(!vis[y]) dfs(y);
}
for(int i = 0; i < (int)mp[x].size(); ++i) {
int y = mp[x][i].first; int z = mp[x][i].second;
for(int j = 1; j <= N; ++j) {
if( dp[x][j] > dp[y][j-1]+z ) {
dp[x][j] = dp[y][j-1]+z;
if(x==N && j > ans && dp[x][j] <= T) ans = j;
}
}
}
vis[x] = 1;
}
void re_dfs(int x,int num) {
op.push_back(x);
for(int i = 0; i < (int)mp[x].size(); ++i) {
int y = mp[x][i].first; int z = mp[x][i].second;
if(dp[x][num] == dp[y][num-1]+z){
re_dfs(y,num-1);
return;
}
}
}
int main(){
while(~scanf("%d %d %d",&N,&M,&T)) {
ans = 0; op.clear();
memset(vis,0,sizeof(vis));
memset(dp,INF,sizeof(dp));
for(int i = 1; i <= N; ++i) mp[i].clear();
for(int i = 1; i <= M; ++i) {
int a,b,c; scanf("%d %d %d",&a,&b,&c);
mp[b].push_back({a,c});
}
dp[1][1] = 0;
dfs(N);
re_dfs(N,ans);
printf("%d\n",ans);
for(int i = (int)op.size()-1; i >= 0; --i) {
printf("%d ",op[i]);
} printf("\n");
}
return 0;
}