迪杰斯特拉求条
查看原帖
迪杰斯特拉求条
1446545
XHZnewlife楼主2024/11/22 20:08

如下码:

#include<bits/stdc++.h>
using namespace std;
int n;
int s,t,a,b;
struct edge{
	int dian;
	double quan;
};
struct no{
	double ans;
	int node;
	bool operator<(const no& other)const{
		return ans>other.ans;
	}
};
struct city{
	int x,y;
};
vector <city> ci;
vector <edge> tu[405];
priority_queue<no> dcl;
int zz=1,cinu=1;
int flag[405]={},ans[405]={};
double walk(int sta,int la){
	dcl.push({0,sta});
	no cl;
	for(int i=1;i<=4*s;i++){
		flag[i]=0;
		ans[i]=INT_MAX;
	}
	ans[sta]=0;
	while(!dcl.empty()){
		cl=dcl.top();
		if(cl.node>la*4-4 and cl.node<=la*4){
			return cl.ans;
		}
		dcl.pop();
		if(flag[cl.node]==1)continue;
		flag[cl.node]=1;
		for(int i=0;i<tu[cl.node].size();i++){
			if(ans[tu[cl.node][i].dian]>cl.ans+tu[cl.node][i].quan){
				ans[tu[cl.node][i].dian]=cl.ans+tu[cl.node][i].quan;
				dcl.push({tu[cl.node][i].dian,ans[tu[cl.node][i].dian]});
			}
		}
	}
}
int main(){
	cin>>n;
	for(int i=0,za,zb;i<n;i++){
		ci.push_back({0,0});
		cin>>s>>t>>a>>b;
		for(int j=0,x[4],y[4],T,xm=0,ym=0;j<s;j++){
			x[3]=-1,y[3]=-1;
			for(int k=0;k<3;k++){
				cin>>x[k]>>y[k];
				if(k==0){
					x[3]=x[k];
					y[3]=y[k];
				}
				if(k==1 and x[3]==x[k])xm=1;
				if(k==1 and y[3]==y[k])ym=1;
				if(k==2 and x[3]==x[k])x[3]=x[1];
				if(k==2 and y[3]==y[k])y[3]=y[1];
				if(k==2 and xm==1)x[3]=x[k];
				if(k==2 and ym==1)y[3]=y[k];
			}
			for(int k=0;k<4;k++){
				ci.push_back({x[k],y[k]});
			}
			cin>>T;
			if(j==a-1)za=zz+1;
			for(int k=0;k<3;k++){
				for(int l=k+1;l<4;l++){
					tu[zz].push_back({zz+l,sqrt((x[k]-y[l])^2+(x[l]-y[k])^2)*T});
					tu[zz+l].push_back({zz,sqrt((x[k]-y[l])^2+(x[l]-y[k])^2)*T});
				}
				zz++;
			}
			zz++;
			for(int k=1;k<zz-4;k++){
				for(int l=zz-4;l<=zz;l++){
					tu[l].push_back({k,sqrt((ci[k].x-ci[l].y)^2+(ci[k].y-ci[l].x)^2)*t});
					tu[k].push_back({l,sqrt((ci[k].x-ci[l].y)^2+(ci[k].y-ci[l].x)^2)*t});
				}
			}
			
		}
		cout<<min(min(walk(za+2,b),walk(za+3,b)),min(walk(za,b),walk(za+1,b)))<<endl;
		for(int i=1;i<=zz;i++){
			tu[i].clear();
		}
		ci.clear();
	}
}

谢谢dalao

2024/11/22 20:08
加载中...