Easy Glide---无向图最短路&&最短路知识点

任意门
Grammy is playing a boring racing game named Easy Gliding. The game’s main content is to reach the destination as fast as possible by walking or gliding. The fastest player wins.

Each player controls a character on a two-dimensional plane. A character can walk at any moment with a speed of V1. Especially, when a character touches a gliding point, he/she can glide with a speed of V2 for the following 3 seconds. It is guaranteed that V1<V2.

Now Grammy locates at point S and she knows the coordinates of all the gliding points p1,p2,…,pn. The goal is to reach point T as fast as possible. Could you tell her the minimum time she has to spend to reach point T?

Input
The first line contains one integer n (1≤n≤1000), denoting the number of gliding points.

The following n lines describe the gliding points. The i-th line contains two integers xi,yi (−1000000≤xi,yi≤1000000), representing the coordinates of the i-th gliding point pi.

The next line contains four integers Sx,Sy,Tx,Ty (−1000000≤Sx,Sy,Tx,Ty≤1000000), representing the coordinates of S and T.

The next line contains two integers V1,V2 (1≤V1<V2≤1000000), representing the speed of walking and gliding.

Output
Output the minimum time Grammy has to spend to reach point T in one line. Your answer will be considered correct if its absolute or relative error does not exceed 10−6.

Examples
input

2
2 1
0 3
0 0 4 0
10 11

output

0.400000000000

input

2
2 1
-2 0
0 0 4 0
1 2

output

3.354101966250

input

2
2 1
-2 0
0 0 4 0
1 10000

output

2.000600000000

这题看起来很难,其实就是最短路,先求出起点到终点,起点到每个滑翔点,各个滑翔点之间,每个滑翔点到终点之间的距离,现在就变成一个典型的最短路问题,直接dijkstra算法解决问题。

最短路问题扩充

最短路可以分为单源最短路,多源汇最短路(起点和终点都不确定)
在这里插入图片描述
稠密图用朴素版dijk算法,稀疏图用堆优化版本。
dijkstra基于贪心,floyd基于动态规划,bellman基于离散数学。
稠密图用邻接矩阵来求,稀疏图用用邻接表来存。

朴素版dijkstra

例题

#include<bits/stdc++.h>
using namespace std;
const int N = 510;

int n, m;
int g[N][N];
int dist[N];
bool st[N];

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;

    for (int i = 0; i < n - 1; i ++ )
    {
        int t = -1;
        for (int j = 1; j <= n; j ++ )
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;

        for (int j = 1; j <= n; j ++ )
            dist[j] = min(dist[j], dist[t] + g[t][j]);

        st[t] = true;//把自环给干掉了
    }

    if (dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    scanf("%d%d", &n, &m);

    memset(g, 0x3f, sizeof g);
    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        //存在重边和自环
        g[a][b] = min(g[a][b], c);
    }

    printf("%d\n", dijkstra());

    return 0;
}

堆优化dijkstra

#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

typedef pair<int, int> PII;

const int N = 1e6 + 10;

int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});

    while (heap.size())
    {
        auto t = heap.top();
        heap.pop();

        int ver = t.second, distance = t.first;

        if (st[ver]) continue;
        st[ver] = true;

        for (int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[ver] + w[i])
            {
                dist[j] = dist[ver] + w[i];
                heap.push({dist[j], j});
            }
        }
    }

    if (dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    scanf("%d%d", &n, &m);

    memset(h, -1, sizeof h);
    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
    }

    cout << dijkstra() << endl;

    return 0;
}


回归到本题:直接用输入顺序的下标来映射输入的横纵坐标值,因为这个图是个完全无向图,所以计算出每两点之间所使用的时间,的然后跑一遍dijstra求最短时间即可。有一个坑是double类型的变量不能用memset进行赋值,用for循环才能实现。因为是无向图,链式前向星的e数组,边数数组和权值数组要开两倍的空间。完全图的建图方法,方法如下。

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
 
#define int long long
 
const int maxn = 1001000;
int n;
int tot;
int v1, v2;
int head[maxn];
double to[maxn];
bool vis[maxn];
 
struct point{
    int x;
    int y;
}p[maxn], s, t;
 
struct edge{
    int to;
    double val;
    int from;
    int nxt;
}e[2 * maxn];
 
double get_dis(const point & a, const point & b){
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
 
void add(int x, int y, double val){
    tot++;
    e[tot].to = y;
    e[tot].from = x;
    e[tot].val = val;
    e[tot].nxt = head[x];
    head[x] = tot;
}
 
void dij(int x){
    for(int i = 1; i <= n + 1; i++) to[i] = 0x3f3f3f3f;
    priority_queue<pair<double, int> > q;
    to[x] = 0;
    q.push(make_pair(-to[x], x));
    while(!q.empty()){
        int u = q.top().second;
        q.pop();
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u]; i != -1; i = e[i].nxt){
            int v = e[i].to;
            if(to[v] > to[u] + e[i].val){
                to[v] = to[u] + e[i].val;
                q.push(make_pair(-to[v], v));
            }
        }    
    }
    
}
 
signed main(){
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> p[i].x >> p[i].y;
    memset(head, -1, sizeof(head));
    cin >> s.x >> s.y >> t.x >> t.y;
    cin >> v1 >> v2;
    for(int i = 1; i < n; i++){//连接每一个滑翔点
        for(int j = i + 1; j <= n; j++){
            double dis = get_dis(p[i], p[j]);
            if(dis > v2 * 3.0){//分情况构造所花时间的无向图
                add(i, j, 3.0 + (dis - v2 * 3.0) / v1);//记录从i到j要花的时间
                add(j, i, 3.0 + (dis - v2 * 3.0) / v1);
            }
            else{
                add(i, j, dis / v2);
                add(j, i, dis / v2);
            }
        }
    }
    for(int i = 1; i <= n; i++){//将起点和终点连接
        add(0, i, get_dis(p[i], s) / v1);
        add(i, 0, get_dis(p[i], s) / v1);
        double dis = get_dis(p[i], t);
        if(dis > v2 * 3.0){
            add(i, n + 1, 3.0 + (dis - v2 * 3.0) / v1);
            add(n + 1, i, 3.0 + (dis - v2 * 3.0) / v1);
        } 
        else{
            add(i, n + 1, dis / v2);
            add(n + 1, i, dis / v2);
        }
    }
    double dis = get_dis(s, t);//连接起点和终点 
    add(0, n + 1, dis / v1);
    add(n + 1, 0, dis / v1);
    dij(0);//最短路
    printf("%.10lf\n", to[n + 1]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值