为什么会T 啊啊? 不是无环么?
查看原帖
为什么会T 啊啊? 不是无环么?
233127
千叶の黑猫楼主2020/12/3 17:04
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 5e3+10;
int h[maxn],cnt;
short t[maxn][maxn];
const int INF = 0x3f3f3f3f;
struct node{
	int to,next,dis;
}e[maxn];
int dp[maxn][maxn];
int n,m,kk,e1,e2,d;
void add(int u,int v,int dis){
	e[++cnt].to=v;
	e[cnt].next=h[u];
	e[cnt].dis=dis;
	h[u]=cnt;
}
void dfs(int u,int k){
	for(int i=h[u];i;i=e[i].next){
		int v=e[i].to;
		if(dp[u][k]+e[i].dis<dp[v][k+1]) {
		dp[v][k+1]=dp[u][k]+e[i].dis;	
		t[v][k+1]=u;
		}
		if(dp[v][k+1]>kk) continue;
		if(v==n) continue;
		dfs(v,k+1);
	}
}
void p(int k,int x){
		if(t[k][x]==1) {
			cout<<1<<" ";
			return ;
		}
		p(t[k][x],x-1);
		printf("%d ",t[k][x]);
}
int main(){
	cin>>n>>m>>kk;
	for(int i=1;i<=m;++i){
		scanf("%d%d%d",&e1,&e2,&d);
		add(e1,e2,d);
	} 
	memset(dp,INF,sizeof(dp));
	dp[1][1]=0;
	dfs(1,1);
	for(int i=5000;i>=1;i--){
		if(dp[n][i]<=kk){
			cout<<i<<endl;
			p(n,i);
			cout<<n<<endl;
			break;
		}
	}
}
2020/12/3 17:04
加载中...