(图论)Car的旅行路线

https://ac.nowcoder.com/acm/problem/16697

#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
inline int lowbit(int x) { return x & (-x); }
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PII pair<int, int>
#define VIT vector<int>
#define x first
#define y second
#define inf 0x3f3f3f3f
#define PDI pair<double, int>
const int N = 410, M = 2e6 + 10;
int h[N], e[M], ne[M], idx;
double w[M], dist[N];
bool st[N];
int S[4], T[4];
int id;
int a, b;

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

struct Node {
    int x, y;
}p[N];

double get_dis(double x1, double y1, double x2, double y2) {
    double dx = x1 - x2;
    double dy = y1 - y2;
    return sqrt(dx * dx + dy * dy);
}

void add1() {
    int _x[4], _y[4], t;
    for (int i = 0; i < 3; ++i) scanf("%d%d", _x + i, _y + i);
    scanf("%d", &t);
    if ((_x[0] - _x[1]) * (_x[2] - _x[1]) + (_y[2] - _y[1]) * (_y[0] - _y[1]) == 0) {
        swap(_x[0], _x[1]);
        swap(_y[0], _y[1]);
    }
    else if ((_x[0] - _x[2]) * (_x[1] - _x[2]) + (_y[1] - _y[2]) * (_y[0] - _y[2]) == 0) {
        swap(_x[0], _x[2]);
        swap(_y[0], _y[2]);
    }
    _x[3] = _x[1] + _x[2] - _x[0], _y[3] = _y[1] + _y[2] - _y[0]; 

    for (int i = 0; i < 4; ++i) p[id + i].x = _x[i], p[id + i].y = _y[i];

    for (int i = id; i < id + 4; ++i)
        for (int j = id; j < id + 4; ++j)
            if (i == j) continue;
            else {
                double d = get_dis(p[i].x, p[i].y, p[j].x, p[j].y);
                add(i, j, d * t);
                add(j, i, d * t);
            }

    if (id / 4 + 1 == a) 
        for (int i = 0; i < 4; ++i) S[i] = id + i;
    else if (id / 4 + 1 == b) 
        for (int i = 0; i < 4; ++i) T[i] = id + i;

    id += 4; 
}

double dijkstra() {
    for (int i = 0; i < id; ++i) dist[i] = 1e18; 
    memset(st, 0, sizeof st); 
    priority_queue<PDI, vector<PDI>, greater<PDI>> q;
    for (int i = 0; i < 4; ++i) {
        dist[S[i]] = 0;
        q.push({0, S[i]});
    }
    while (q.size()) {
        PDI t = q.top();
        q.pop();
        int ver = t.y; double dis = t.x;
        if (st[ver]) continue;
        st[ver] = true;
        for (int i = h[ver]; ~i; i = ne[i]) {
            int j = e[i];
            if (dist[j] > dis + w[i]) {
                dist[j] = dis + w[i];
                q.push({dist[j], j});
            }
        }
    }
    double res = 1e18;
    for (int i = 0; i < 4; ++i) res = min(res, dist[T[i]]);
    return res;
}


int main() {
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    //IO;
    int _;
    cin >> _;
    while (_--) {
        id = idx = 0;
        memset(p, 0, sizeof p);
        memset(h, -1, sizeof h);
        int s, t;
        scanf("%d%d%d%d", &s, &t, &a, &b);
        for (int i = 1; i <= s; ++i) add1();
        for (int i = 0; i < id; ++i) 
            for (int j = 0; j < id; ++j)
                if (i / 4 != j / 4) {
                    double d = get_dis(p[i].x, p[i].y, p[j].x, p[j].y);
                    add(i, j, d * t);
                    add(j, i, d * t);
                }
        double ans = dijkstra(); 
        printf("%.1lf\n", ans); 
    }
    return 0;
}



posted @ 2021-03-02 18:04  phr2000  阅读(95)  评论(0编辑  收藏  举报