#2#5WA求助!
查看原帖
#2#5WA求助!
352666
张恒灿楼主2023/2/6 12:27

RT,求助

#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#define INF
typedef long long ll;
using namespace std;
const int maxn = 105;
int n, t, A, B, tmpx, tmpy;
double d[maxn][5][maxn][5];
bool vis[maxn][5];

struct city { //城市
	int x[5], y[5];
	int T;
} ct[maxn];

struct edge {
	int vnode, vid;
	double w;
};
vector<edge> adj[maxn][5];

struct node {
	int town_node, town_id;
	double dis;
	bool operator<(const node &tt)const {
		return dis > tt.dis;
	}
};

void Dijkstra(int start_node, int start_id) {//第几个城市的第几个机场
	memset(vis, 0, sizeof(vis));

	for (int i = 1; i <= n; i++) {
		d[start_node][start_id][i][1] = 2147483647;
		d[start_node][start_id][i][2] = 2147483647;
		d[start_node][start_id][i][3] = 2147483647;
		d[start_node][start_id][i][4] = 2147483647;
	}

	priority_queue<node> q;
	q.push({start_node, start_id, 0});
	d[start_node][start_id][start_node][start_id] = 0;

	while (!q.empty()) {
		int unode = q.top().town_node, uid = q.top().town_id;
		q.pop();

		if (vis[unode][uid])
			continue;

		vis[unode][uid] = 1;

		for (int i = 0; i < adj[unode][uid].size(); i++) {
			int vnode = adj[unode][uid][i].vnode;
			int vid = adj[unode][uid][i].vid;
			double w = adj[unode][uid][i].w;

			if (d[start_node][start_id][vnode][vid] > d[start_node][start_id][unode][uid] + w) {
				d[start_node][start_id][vnode][vid] = d[start_node][start_id][unode][uid] + w;
				q.push({vnode, vid, d[start_node][start_id][vnode][vid]});
			}
		}
	}

	return;
}

void qiu(int x1, int y1, int x2, int y2, int x3, int y3) {//求第四个点
	if (x1 == x2)
		tmpx = x3;
	else if (x1 == x3)
		tmpx = x2;
	else
		tmpx = x1;

	if (y1 == y2)
		tmpy = y3;
	else if (y1 == y3)
		tmpy = y2;
	else
		tmpy = y1;
}

double distance(int node1, int id1, int node2, int id2) {//求两点之间的距离
	double x1 = ct[node1].x[id1], y1 = ct[node1].y[id1], x2 = ct[node2].x[id2], y2 = ct[node2].y[id2];
	return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

double mindb(double tmp1, double tmp2) {
	return tmp1 < tmp2 ? tmp1 : tmp2;
}

int main() {
	int TTT;
	cin >> TTT;

	while (TTT--) {
		cin >> n >> t >> A >> B;

		for (int i = 1; i <= n; i++) {
			cin >> ct[i].x[1] >> ct[i].y[1] >> ct[i].x[2] >> ct[i].y[2] >> ct[i].x[3] >> ct[i].y[3] >> ct[i].T;
			qiu(ct[i].x[1], ct[i].y[1], ct[i].x[2], ct[i].y[2], ct[i].x[3], ct[i].y[3]); //求第四个点
			ct[i].x[4] = tmpx, ct[i].y[4] = tmpy;
		}

		//求两点之间的距离
		for (int node1 = 1; node1 <= n; node1++) {
			for (int id1 = 1; id1 <= 4; id1++) {
				for (int node2 = 1; node2 <= n; node2++) {
					for (int id2 = 1; id2 <= 4; id2++) {
						if (node1 == node2 && id1 == id2)//同一个点
							continue;

						if (node1 == node2) { //两个城市一样,公路
							double w = ct[node1].T * distance(node1, id1, node2, id2);
							adj[node1][id1].push_back({node2, id2, w});
						} else { //两个城市不一样,航线
							double w = t * distance(node1, id1, node2, id2);
							adj[node1][id1].push_back({node2, id2, w});
						}
					}
				}
			}
		}

		Dijkstra(A, 1);
		Dijkstra(A, 2);
		Dijkstra(A, 3);
		Dijkstra(A, 4);
		double minn = 2147483647;

		for (int i = 1; i <= 4; i++) {
			for (int j = 1; j <= 4; j++) {
				minn = mindb(minn, d[A][i][B][j]);
			}
		}

		cout << fixed << setprecision(1) << minn << endl;
	}

	return 0;
}

样例2数据:

输入数据
1
3 10 1 3
2 2 2 1 1 2 10
2 12 12 2 22 12 1
22 22 22 32 32 22 10

标准答案
214.1

我的答案
122.4
2023/2/6 12:27
加载中...