Luogu-P1027 Car的旅行路线 已知三点确定矩形 + 最短路

Posted ckxkexing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu-P1027 Car的旅行路线 已知三点确定矩形 + 最短路相关的知识,希望对你有一定的参考价值。

传送门:https://www.luogu.org/problemnew/show/P1027

 

题意:

    图中有n个城市,每个城市有4个机场在矩形的四个顶点上。一个城市间的机场可以通过高铁通达,不同城市间要通过飞机。现在问从s到t城市最少需要多少的费用。

思路:

    已知矩形的三个顶点,可以用勾股定理确定斜边后,利用平行四边形原理——两对对角顶点的x之和是相同的,y之和也是相同的得到第四个顶点。然后用求最短路的dji即可。

  

技术分享图片
#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>

using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "
";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
typedef pair<double,int>pdi;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘
‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
#define max3(a,b,c) max(max(a,b), c); 
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;       
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
ll mod = 10007;
const double esp = 1e-8;
const double PI=acos(-1.0);
const double PHI=0.61803399;    //黄金分割点
const double tPHI=0.38196601;


template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/
            const int maxn = 500;
            int c1,s,t,n;
            double dis[maxn];
            struct node
            {
                int x,y,bl;                
                int cst;
            }a[maxn];
            void getp(int x1,int y1,int x2,int y2,int x3,int y3,int i){
                int ab = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
                int ac = (x1-x3)*(x1-x3) + (y1-y3)*(y1-y3);
                int bc = (x2-x3)*(x2-x3) + (y2-y3)*(y2-y3);
                int x4,y4;

                if(ab + ac == bc) x4 = x2 + x3 - x1, y4 = y2 + y3 - y1;
                if(ab + bc == ac) x4 = x1 + x3 - x2, y4 = y1 + y3 - y2;
                if(ac + bc == ab) x4 = x1 + x2 - x3, y4 = y1 + y2 - y3;
                a[i+3].x = x4;
                a[i+3].y = y4;
            }
            double getdis(int i,int j){
                return sqrt(1.0*(a[i].x - a[j].x)*(a[i].x - a[j].x) + 1.0*(a[i].y - a[j].y)*(a[i].y - a[j].y));
            }
            void dji(){
                for(int i=1; i<=4*n; i++)dis[i] = 1000000000.9;
                dis[(s-1) * 4+1] = dis[(s-1) * 4+2] = dis[(s-1) * 4+3] = dis[(s-1) * 4 + 4] =0;
                priority_queue<pdi>que;
                que.push(pdi(0.0,(s-1) * 4+1));
                que.push(pdi(0.0,(s-1) * 4+2));
                que.push(pdi(0.0,(s-1) * 4+3));
                que.push(pdi(0.0,(s-1) * 4+4));
                while(!que.empty()){
                    pdi tmp = que.top(); que.pop();
                    if(dis[tmp.se] < -1*tmp.fi)continue;
                    for(int i=1; i<=4*n; i++){
                        if(tmp.se != i){
                            double d = getdis(tmp.se, i);
                            if(a[tmp.se].bl == a[i].bl) {
                                if(dis[i] > dis[tmp.se] +1.0* a[i].cst * d){
                                    dis[i] = dis[tmp.se] + 1.0*a[i].cst * d;
                                    que.push(pdi(-dis[i],i));
                                }
                            }
                            else {
                                if(dis[i] > dis[tmp.se] + 1.0*c1 * d){
                                    dis[i] = dis[tmp.se] + 1.0*c1 * d;
                                    que.push(pdi(-dis[i],i));
                                }
                            }
                        }
                    }
                }

            }
int main(){
            int T;  scanf("%d", &T);
            while(T--){
                scanf("%d%d%d%d",&n, &c1, &s, &t);
                for(int i=1; i<=4*n; i+=4){
                    int cst;
                    scanf("%d%d%d%d%d%d%d", &a[i].x,&a[i].y,&a[i+1].x, &a[i+1].y,&a[i+2].x, &a[i+2].y,&cst);
                    getp(a[i].x, a[i].y,a[i+1].x, a[i+1].y,a[i+2].x, a[i+2].y,i);
                    a[i+3].cst = a[i].cst = a[i+1].cst = a[i+2].cst = cst;
                    a[i+3].bl = a[i].bl = a[i+1].bl = a[i+2].bl = (i-1)/4 + 1;
                }

                dji();
                double ans = 1000000000.9;
                ans = min(ans, dis[(t-1)*4+1]);
                ans = min(ans, dis[(t-1)*4+2]);
                ans = min(ans, dis[(t-1)*4+3]);
                ans = min(ans, dis[(t-1)*4+4]);
                printf("%.1f
", ans);
                
            }   
            return 0;
}
P1027

 

以上是关于Luogu-P1027 Car的旅行路线 已知三点确定矩形 + 最短路的主要内容,如果未能解决你的问题,请参考以下文章

洛谷 P1027Car的旅行路线

最短路luoguP1027Car的旅行路线

洛谷 P1027 Car的旅行路线

luogu P1027 Car的旅行路线

luogu P1027 Car的旅行路线

落谷 P1027 Car的旅行路线