Luo Gu P1433 eat cheese + memory of heuristic search

This question is to eat cheese, the original data is small, but the Valley of Los administrator greatly enhanced,

DFS card out successfully practice solution to a problem number one.

Ha ha ha say good bare title search, search a practice card out is several meanings ?

I also wrote a DFS discovery was later turned card solution to a problem,

And then found the number one problem and solution approach is not exactly the same as I do?

copy down run down the local data 10,

Found ran 8 seconds. . .

Problem is seemingly only to find the solution.

The next time an administrator much more trouble cancer data, update about the solution to a problem it really ?

Then rolled his solution to a problem and found that all the time are very ancient,

That approach seems to have all run, but dfs. . .

Then I ask why you want to put the question in a single search,

Instead dp years?


But I dignified heavenly OIer, how can you give up DFS burst search?

So painstaking research, and finally made a practice of heuristic search.


note:

Heuristic search is not a positive solution ,


Because the data

Cancer ,

Then we do questions people have

More cancer!


Give full play to my heavenly OIer data-oriented programming traditional virtues ,

You can use memory ( as the name suggests a two-dimensional array of storage)

The first represents the point i, the second dimension represents the number of steps step,

What memory do?

Storage shortest distance,

If you later access to this point,

The current will have to go the distance compare with the shortest distance storage array,

If it is less than the update, is greater than on the return


We found that this is the memory of + greedy thoughts,

But the memory of greed is not right,

That every step of the choices have after-effect ,

Perhaps the shortest distance you walk step to step i point is correct,

But it does not guarantee take the shortest path step to step I + 1 + 1 (may be any other point) is memory [i] [step] + distance [i] [i + 1]

then what should we do? (At this point the coach might remind you use dynamic programming, and turn dp positive solution)

But I just want to use DFS how to do it?

Then the call out today's hero - the heuristic search,

In simple terms, heuristic search is to take and do not take all the analysis, better solution from select (or deleting invalid solutions)

The key is this line of code:

	if(s<memory[i][step]+s2)
	{
		memory[i][step]=s;
	}

What s2 here to take it?

Valley because I can not download the data point test,

So after some thinking ( mess ) after

Found s2 taken sqrt (0.8) can be obtained by all test points

(The value of s2 as the case may be, to the small speed may take pursuit. Pursue sound to be taken large)

And the entire program run fast - 53ms

Even beyond positive solutions dp 171ms

I refuse to record and compare data before transformation

Code offer AC

#include<bits/stdc++.h>
using namespace std;
int N;
struct pue
{
	double a,b;
}; 
pue w[22];
double ans=INT_MAX;
double distance[30][35];
double memory[30][30];
bool vis[25];
double s2=sqrt(0.8);
double calculate(double x1,double y1,double x2,double y2)
{
	double ke=x1-x2,kr=y1-y2;
	return sqrt(ke*ke+kr*kr);
}
void yuchuli()
{
	for(int i=0;i<=N;i++)
	for(int j=0;j<=N;j++)
	{
		memory[i][j]=INT_MAX;
		distance[i][j]=calculate(w[i].a,w[i].b,w[j].a,w[j].b);
	}
}
void dfs(double s,int i,int step)
{
	if(s<memory[i][step]+s2)
	{
		memory[i][step]=s;
	}
	else
	return ;
	if(step==N)
	{
	if(ans>s)ans=s;
	return ;	
	}
	for(int j=1;j<=N;j++)
	{
		if(i==j)continue;
		if(vis[j]==0)
		{
		double k=s+distance[i][j];
		if(k<ans)
		{
			vis[j]=1;
			dfs(k,j,step+1);
			vis[j]=0;
		}
		}
		
		
	}
}
bool com(pue x,pue y)
{
	if(x.a!=y.a)return x.a>y.a;
	else
	return x.b>y.b;
}
int main()
{
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
	{
		scanf("%lf%lf",&w[i].a,&w[i].b);
	}
	sort(w+1,w+1+N,com);//这里需要从大到小进行排序
	yuchuli();
	dfs(0.0,0,0);
	printf("%.2f",ans);
    return 0;
}

Again, this is not a positive solution! ! !

The correctness of data via the full range set,

People just change my topic and big data, may be able to swap cards,

(However, other numbers can try to take s2)

Just I think since it is a search problem, you must have a search solution, or how worthy of the title of it?

At the same time, also to tell you, sometimes using heuristic search found deep in the examination room to play violent wonders.

I hope you can gain something.

If you think the author wrote okay, you point a praise it!


Guess you like

Origin www.cnblogs.com/poised/p/12597794.html