关于DFS爆搜
查看原帖
关于DFS爆搜
243063
cyx20080216楼主2021/8/28 16:18

从1号点开始搜索,当当前耗时大于当前最优解时剪枝。其他测试点耗时都在毫秒级,唯独8号点被卡到超时是为什么?

Code:

#include<bits/stdc++.h>
using namespace std;
struct line
{
	int to;
	double l,c;
	line(const int &to=0,const double &l=0.0,const double &c=1e9):
		to(to),
		l(l),
		c(c)
	{
	}
};
int n,m;
double x;
list<line> maps[501];
bool flag[501];
double ans=1e9;
inline void init()
{
	scanf("%d %d %lf",&n,&m,&x);
	for(int i=1;i<=m;i++)
	{
		int from,to;
		double l,c;
		scanf("%d %d %lf %lf",&from,&to,&l,&c);
		maps[from].push_front(line(to,l,c));
		maps[to].push_front(line(from,l,c));
	}
}
void dfs(const int &at,const double &l,const double &c)
{
	if(l+x/c>=ans)
		return;
	if(at==n)
	{
		ans=l+x/c;
		return;
	}
	for(list<line>::const_iterator i=maps[at].begin();i!=maps[at].end();i++)
	{
		int new_at=i->to;
		if(!flag[new_at])
		{
			flag[new_at]=1;
			dfs(new_at,l+i->l,min(c,i->c));
			flag[new_at]=0;
		}
	}
}
int main()
{
	init();
	flag[1]=1;
	dfs(1,0,1e9);
	flag[1]=0;
	printf("%d\n",(int)(ans));
	return 0;
}

Record

2021/8/28 16:18
加载中...