80分求助
查看原帖
80分求助
517223
ThebestJoe楼主2022/10/22 23:34
#include <iostream>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath> 
#include<iomanip>
using namespace std; 
const int maxn=1e3+10;
int head[maxn];
struct node
{
	int to,next;
	double w;
}e[2*maxn];
int cnt;
void  add(int u,int v,double w)
{
	e[++cnt].to=v;
	e[cnt].next=head[u];
	e[cnt].w=w;
	head[u]=cnt;
	
}
int vis[maxn][maxn];
int book[maxn];
double dis[maxn];
int n;
void dijkstra()
{
	
	for(int k=0;k<=n+1;k++)
	{
		dis[k]=1e6;
	}
	dis[0]=0.0;
	
	int t=-1;
	for(int i=0;i<n+1;i++)
	{
		double maxd=1e6;
		for(int j=0;j<=n+1;j++)
		{
			if(dis[j]<maxd&&!book[j])
			{
				maxd=dis[j];
				t=j;
			}
		}
		book[t]=1;
		for(int i=head[t];i;i=e[i].next)
		{
			if(dis[e[i].to]>dis[t]+e[i].w)
			{
				dis[e[i].to]=dis[t]+e[i].w;
			}
		}
	}
}
double dcost,rcost;
struct node2
{
	double x,y;
};
node2 dot[maxn];
double x,y;
int u,v;
int main() { 
     
     cin>>dcost>>rcost;
     cin>>n;
     for(int i=1;i<=n;i++)
     {
	 	cin>>dot[i].x>>dot[i].y;
	 }
	 while(cin>>u>>v&&u!=0&&v!=0)
	 {
	 	vis[u][v]=1;
	 	vis[v][u]=1;
	 }
	 cin>>dot[0].x>>dot[0].y;
	 cin>>dot[n+1].x>>dot[n+1].y;
	 for(int i=0;i<=n+1;i++)
	 {
	 	for(int j=0;j<=n+1;j++)
	 	{
		 	if(i==j)continue;
		 	if(vis[i][j])
		 	{
			 	double len=sqrt(pow(dot[i].y-dot[j].y,2)+pow(dot[i].x-dot[j].x,2));
			 	add(i,j,len*rcost);
			}
			else 
			{
			    double len=sqrt(pow(dot[i].y-dot[j].y,2)+pow(dot[i].x-dot[j].x,2));
				add(i,j,len*dcost);
			}
		}
	 }
	 dijkstra();
	 cout<<setiosflags(ios::fixed)<<setprecision(4)<<dis[n+1];
	 return 0; 
}
2022/10/22 23:34
加载中...