算法训练 Car的旅行路线

问题描述
  又到暑假了,住在城市A的Car想和朋友一起去城市B旅游。她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一 条笔直的高速铁路,第I个城市中高速铁路了的单位里程价格为Ti,任意两个不同城市的机场之间均有航线,所有航线单位里程的价格均为t。
  那么Car应如何安排到城市B的路线才能尽可能的节省花费呢?她发现这并不是一个简单的问题,于是她来向你请教。
  找出一条从城市A到B的旅游路线,出发和到达城市中的机场可以任意选取,要求总的花费最少。
输入格式
  的第一行有四个正整数s,t,A,B。
  S表示城市的个数,t表示飞机单位里程的价格,A,B分别为城市A,B的序号,(1<=A,B<=S)。
  接下来有S行,其中第I行均有7个正整数xi1,yi1,xi2,yi2,xi3,yi3,Ti,这当中的(xi1,yi1),(xi2,yi2),(xi3,yi3)分别是第I个城市中任意三个机场的坐标,T I为第I个城市高速铁路单位里程的价格。
输出格式
  共有n行,每行一个数据对应测试数据,保留一位小数。
样例输入
3 10 1 3
1 1 1 3 3 1 30
2 5 7 4 5 2 1
8 6 8 8 11 6 3
样例输出
47.55
数据规模和约定
  0<S<=100,

又是一道样例错的题,没有几个,直接输入

#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#define bug(x) printf("%d***\n",x)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
using namespace std;
typedef long long ll;

const int maxn=310;
const double eps=1e-6;
double mp[maxn][maxn];
double mon[maxn];
struct Point{
	double x,y;
	Point(){}
	Point(double _x,double _y){
		x=_x,y=_y;
	}
}p[maxn];

int n,A,B;
double t;
/*
3 10 1 3
1 1 1 3 3 1 30
2 5 7 4 5 2 1
8 6 8 8 11 6 3
*/
int check(Point a,Point b,Point c,int pos){
	double tmp=(b.x-a.x)*(c.x-a.x)+(b.y-a.y)*(c.y-a.y);
	if(fabs(tmp)<eps){
		p[pos].x=b.x+c.x-a.x;
		p[pos].y=b.y+c.y-a.y;
		return 1; 
	} 
	return 0;
}

void get_point(int pos){
	Point a=p[pos-1],b=p[pos-2],c=p[pos-3];
	if(check(a,b,c,pos)){
		return;		
	}
	if(check(b,a,c,pos)){
		return;
	}
	if(check(c,a,b,pos)){
		return;
	}
}

void cal(int a,int b){
	double dis=sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y));
	if(b/4==a/4){
		mp[a][b]=mp[b][a]=dis*mon[a/4];
	}
	else{
		mp[a][b]=mp[b][a]=dis*t;
	}
}

int main(){
	scanf("%d %lf %d %d",&n,&t,&A,&B);
	for(int i=0;i<n;i++){
		double x,y;
		for(int j=0;j<3;j++){
			scanf("%lf %lf",&x,&y);
			p[i*4+j]=(Point){x,y};
		}
		get_point(i*4+3);//
		//printf("x:%.1f y:%.1f\n",p[i*4+3].x,p[i*4+3].y);
		scanf("%lf",&mon[i]);
	}
	for(int i=0;i<4*n;i++){
		mp[i][i]=0.0;
		for(int j=i+1;j<4*n;j++){
			cal(i,j);
		}
	}
	for(int k=0;k<4*n;k++){
		for(int i=0;i<4*n;i++){
			for(int j=0;j<4*n;j++){
				mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
			}
		}
	}
	double ans=mp[(A-1)*4][(B-1)*4];
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			ans=min(ans,mp[i+(A-1)*4][j+(B-1)*4]);
		}
	}
	printf("%.1f\n",ans);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值