浅浅求助一下【洛谷MGOI T3】

P9504 『MGOI』Simple Round I | C. 魔法禁林 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
题面↑
本蒟蒻赛时才做11分 :sweat_smile: 赛后改了改 还是TLE(57分)
有大佬说要记忆化搜索(?)
我的代码(剪俩枝的无标记dfs):

#include<bits/stdc++.h>
using namespace std;
int n,m,s,t,res=1e7;
map<int,map<int,int> > mp;
void dfs(int x,int cnt,int health)
{
	if(cnt>=n) return;
	if(health>=res) return;
	if(x==s){
		res=min(res,health);return;
	}
	for(auto it=mp[x].begin();it!=mp[x].end();it++){
		dfs(it->first,cnt+1,health+mp[x][it->first]/(cnt+1));
	}
}
signed main()
{
	int u,v,w;
	cin>>n>>m>>s>>t;
	for(int i=1;i<=m;i++)
	{
		cin>>u>>v>>w;
		mp[u][v]=w;
		mp[v][u]=w;
	}
	dfs(t,0,0);
	cout<<res<<endl;
	return 0;
}

有没有过的大佬看一下 :pray:

1 个赞

建议写 dij