求助!样例没过,20分
查看原帖
求助!样例没过,20分
247173
木易、楼主2022/10/24 16:44
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath> 
#include<queue>
using namespace std;
inline int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')
		{
			w=-1;
		}
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*w;
}
int n,s,t,st,en;
struct node{
	int x1,y1;
	int x2,y2;
	int x3,y3;
	int x4,y4;
	int gs_cost;
}a[105];
vector <int> b[105];
double dis[5][105];
int vis[5][105];
void jc_4(int i)
{
	int w1=(a[i].x1-a[i].x2)*(a[i].x1-a[i].x2)+(a[i].y1-a[i].y2)*(a[i].y1-a[i].y2);
	int w2=(a[i].x3-a[i].x2)*(a[i].x3-a[i].x2)+(a[i].y3-a[i].y2)*(a[i].y3-a[i].y2);
	int w3=(a[i].x1-a[i].x3)*(a[i].x1-a[i].x3)+(a[i].y1-a[i].y3)*(a[i].y1-a[i].y3);
	if(w1==w2+w3)
	{
		a[i].x4=a[i].x1+a[i].x2-a[i].x3;
		a[i].y4=a[i].y1+a[i].y2-a[i].y3;
		return;
	}
	if(w2==w1+w3)
	{
		a[i].x4=a[i].x3+a[i].x2-a[i].x1;
		a[i].y4=a[i].y3+a[i].y2-a[i].y1;
		return;
	}
	if(w3==w2+w1)
	{
		a[i].x4=a[i].x1+a[i].x3-a[i].x2;
		a[i].y4=a[i].y1+a[i].y3-a[i].y2;
		return;
	}
}
double dist(int x1,int x2,int y1,int y2)
{
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
void spfa()
{
	queue <int> q;
	queue <int> q_bh;
	for(int i=1;i<=4;i++)
	{
		for(int j=1;j<=s;j++)
		{
			dis[i][j]=999999.999999;
		}
	}
	q.push(st);
	q_bh.push(1);
	q.push(st);
	q_bh.push(2);
	q.push(st);
	q_bh.push(3);
	q.push(st);
	q_bh.push(4);
	vis[1][st]=vis[2][st]=vis[3][st]=vis[4][st]=1;
	dis[1][st]=dis[2][st]=dis[3][st]=dis[4][st]=0;
	while(!q.empty())
	{
		int x=q.front(),x_bh=q_bh.front();
		q.pop(),q_bh.pop();
		vis[x_bh][x]=0;
		int u,v;
		if(x_bh==1)
		{
			u=a[x].x1;
			v=a[x].y1;
		}
		if(x_bh==2)
		{
			u=a[x].x2;
			v=a[x].y2;
		}
		if(x_bh==3)
		{
			u=a[x].x3;
			v=a[x].y3;
		}
		if(x_bh==4)
		{
			u=a[x].x4;
			v=a[x].y4;
		}
		for(int i=1;i<=n;i++)
		{
			if(x!=i||x_bh!=1)
			{
				double dis_cost=dist(u,a[i].x1,v,a[i].y1);
				if(i==x)
				{
					dis_cost*=a[i].gs_cost;
				}
				else
				{
					dis_cost*=t;
				}
				if(dis[1][i]>dis[x_bh][x]+dis_cost)
				{
					dis[1][i]=dis[x_bh][x]+dis_cost;
					if(vis[1][i]==0)
					{
						q.push(i);
						q_bh.push(1);
						vis[1][i]=1;
					}
				}
			}
			if(x!=i||x_bh!=2)
			{
				double dis_cost=dist(u,a[i].x2,v,a[i].y2);
				if(i==x)
				{
					dis_cost*=a[i].gs_cost;
				}
				else
				{
					dis_cost*=t;
				}
				if(dis[2][i]>dis[x_bh][x]+dis_cost)
				{
					dis[2][i]=dis[x_bh][x]+dis_cost;
					if(vis[2][i]==0)
					{
						q.push(i);
						q_bh.push(2);
						vis[2][i]=1;
					}
				}
			}
			if(x!=i||x_bh!=3)
			{
				double dis_cost=dist(u,a[i].x3,v,a[i].y3);
				if(i==x)
				{
					dis_cost*=a[i].gs_cost;
				}
				else
				{
					dis_cost*=t;
				}
				if(dis[3][i]>dis[x_bh][x]+dis_cost)
				{
					dis[3][i]=dis[x_bh][x]+dis_cost;
					if(vis[3][i]==0)
					{
						q.push(i);
						q_bh.push(3);
						vis[3][i]=1;
					}
				}
			}
			if(x!=i||x_bh!=4)
			{
				double dis_cost=dist(u,a[i].x4,v,a[i].y4);
				if(i==x)
				{
					dis_cost*=a[i].gs_cost;
				}
				else
				{
					dis_cost*=t;
				}
				if(dis[4][i]>dis[x_bh][x]+dis_cost)
				{
					dis[4][i]=dis[x_bh][x]+dis_cost;
					if(vis[4][i]==0)
					{
						q.push(i);
						q_bh.push(4);
						vis[4][i]=1;
					}
				}
			}
		}
	}
}
int main()
{
	n=read();
	while(n--)
	{
		s=read(),t=read(),st=read(),en=read();
		for(int i=1;i<=s;i++)
		{
			a[i].x1=read(),a[i].y1=read();
			a[i].x2=read(),a[i].y2=read();
			a[i].x3=read(),a[i].y3=read();
			a[i].gs_cost=read();
			jc_4(i);
		}
		spfa();
		printf("%.1lf\n",min(dis[1][en],min(dis[2][en],min(dis[3][en],dis[4][en]))));
	}
	return 0;
}
2022/10/24 16:44
加载中...