记忆化搜索,50分求救(结果不对)
查看原帖
记忆化搜索,50分求救(结果不对)
1235672
ch1000ForCompete楼主2024/6/25 13:50
#include<iostream>
#include<cstring>
using namespace std;
const int N=2e4+5;
int h[N],e[N*2],d[N*2],ne[N*2],idx;
int n,m,s,t;
int f[N];
int rec[N];
bool st[N];
int dp(int v){
	if(v==t)return 0;
	if(f[v])return f[v];
	int crowd=2e9;
	for(int i=h[v];i!=-1;i=ne[i]){
		int j=e[i],dist=d[i];
		if(!st[j]){
			st[j]=true;
			crowd=min(crowd,max(dp(j),dist));
			st[j]=false;
		}
	}
	return f[v]=crowd;
}

void add(int a,int b,int c){
	d[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx,idx++;
}

int main(){
	memset(h,-1,sizeof h);
	cin>>n>>m>>s>>t;
	for(int i=0;i<m;i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		add(a,b,c);
		add(b,a,c);
	}
	st[s]=true;
	cout<<dp(s);
}
2024/6/25 13:50
加载中...