方法思路
确定所有机场的坐标:每个城市有4个机场,给定3个点,需要计算第4个点。这三个点应该构成一个直角,利用矩形性质可以求出第4个点。
构建图模型:每个机场是一个节点。同一城市的四个机场之间通过高速铁路相连,铁路费用是距离乘以该城市的铁路单位价格。不同城市的任意机场之间通过航线相连,费用是距离乘以统一的航空单位价格。
计算最短路径:使用 算法计算从城市A的所有机场到城市B的所有机场的最短路径,选择其中的最小值作为结果。
代码
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
};
struct Edge {
int to;
double cost;
Edge(int t, double c) : to(t), cost(c) {}
};
struct Node {
int in;
double cost;
Node(int i, double c) : in(i), cost(c) {}
bool operator>(const Node& other) const {
return cost > other.cost;
}
};
Point fp(Point p1, Point p2, Point p3) {
Point p4;
long long d1 = (p2.x - p1.x) * (p3.x - p1.x) + (p2.y - p1.y) * (p3.y - p1.y);
long long d2 = (p1.x - p2.x) * (p3.x - p2.x) + (p1.y - p2.y) * (p3.y - p2.y);
long long d3 = (p1.x - p3.x) * (p2.x - p3.x) + (p1.y - p3.y) * (p2.y - p3.y);
if (d1 == 0) {
p4.x = p2.x + p3.x - p1.x;
p4.y = p2.y + p3.y - p1.y;
} else if (d2 == 0) {
p4.x = p1.x + p3.x - p2.x;
p4.y = p1.y + p3.y - p2.y;
} else {
p4.x = p1.x + p2.x - p3.x;
p4.y = p1.y + p2.y - p3.y;
}
return p4;
}
double cd(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
void solve() {
int n;
cin >> n;
while (n--) {
int S, t, A, B;
cin >> S >> t >> A >> B;
vector<vector<Point>> c(S);
vector<int> Ts(S);
for (int i = 0; i < S; ++i) {
Point p1, p2, p3;
cin >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y >> Ts[i];
Point p4 = fp(p1, p2, p3);
c[i].push_back(p1);
c[i].push_back(p2);
c[i].push_back(p3);
c[i].push_back(p4);
}
vector<Point> at;
vector<int> co;
for (int i = 0; i < S; ++i) {
for (int j = 0; j < 4; ++j) {
at.push_back(c[i][j]);
co.push_back(i);
}
}
int ta = at.size();
vector<vector<Edge>> graph(ta);
for (int i = 0; i < ta; ++i) {
int city_i = co[i];
for (int j = i + 1; j < ta; ++j) {
if (co[j] == city_i) {
double dist = cd(at[i], at[j]);
double cost = dist * Ts[city_i];
graph[i].emplace_back(j, cost);
graph[j].emplace_back(i, cost);
}
}
}
for (int i = 0; i < ta; ++i) {
for (int j = i + 1; j < ta; ++j) {
if (co[i] != co[j]) {
double dist = cd(at[i], at[j]);
double cost = dist * t;
graph[i].emplace_back(j, cost);
graph[j].emplace_back(i, cost);
}
}
}
double mc = 1e18;
priority_queue<Node, vector<Node>, greater<Node>> pq;
vector<double> dist(ta, 1e18);
int start_city = A - 1;
int ec = B - 1;
for (int i = 0; i < ta; ++i) {
if (co[i] == start_city) {
dist[i] = 0;
pq.emplace(i, 0);
}
}
while (!pq.empty()) {
Node node = pq.top();
pq.pop();
int u = node.in;
if (node.cost > dist[u]) continue;
for (const Edge& e : graph[u]) {
int v = e.to;
if (dist[v] > dist[u] + e.cost) {
dist[v] = dist[u] + e.cost;
pq.emplace(v, dist[v]);
}
}
}
for (int i = 0; i < ta; ++i) {
if (co[i] == ec) {
if (dist[i] < mc) {
mc = dist[i];
}
}
}
cout << fixed << setprecision(1) << mc;
}
}
int main() {
solve();
return 0;
}没打注释,方便复制
求过
