20分求条,玄关
查看原帖
20分求条,玄关
973237
cyhyeee楼主2024/7/17 11:34
#include <bits/stdc++.h>
using namespace std;
const double inf = double(INT_MAX);
vector <int> v(2005), pre(2005);
vector <double> d(2005, inf);
struct awa {
	int x, y;
}a[2005];
struct ovo {
	int x;
	double w;
	bool operator < (const ovo x) const {
		return w > x.w;
	}
};
vector <ovo> e[2005];
double get(int x, int y) {
	double tmp = sqrt((a[x].x - a[y].x) * (a[x].x - a[y].x) + (a[x].y - a[y].y) * (a[x].y - a[y].y));  
	return tmp;
}
priority_queue <ovo> pq; 
signed main() {
	int n, m; cin >> n >> m;
	for (int i = 1; i <= n; i ++) {
		cin >> a[i].x >> a[i].y;
	}
	for (int i = 1; i <= m; i ++) {
		int x, y; cin >> x >> y;
		double z = get(x, y);
		e[x].push_back({y, z});
		e[y].push_back({x, z});
	}
	d[1] = 0;
	pq.push({1, 0}); 
	while (!pq.empty()) {
		ovo tmp = pq.top(); pq.pop();
		if (v[tmp.x])
			continue;
		v[tmp.x] = 1;
		for (int i = 0; i < e[tmp.x].size(); i ++) {
			int j = e[tmp.x][i].x;
			if (d[j] > e[tmp.x][i].w + tmp.w) {
				d[j] = tmp.w + e[tmp.x][i].w;
				pq.push({j, d[j]});
				pre[j] = tmp.x;
			}
		}
	}
	
	vector <int> val;
	int next = pre[n];
	val.push_back(n);
	while (next) {
		val.push_back(next);
		next = pre[next];
	}
	double ans = double(INT_MAX);
	for (int i = 1; i < val.size(); i ++) {
		fill(v.begin(), v.end(), 0);
		fill(d.begin(), d.end(), inf);
		d[1] = 0;
		pq.push({1, 0});
		while (!pq.empty()) {
			ovo tmp = pq.top(); pq.pop();
			if (v[tmp.x])
				continue;
			v[tmp.x] = 1;
			for (int i = 0; i < e[tmp.x].size(); i ++) {
				if (tmp.x == val[i - 1] && e[tmp.x][i].x == val[i] || e[tmp.x][i].x == val[i - 1] && tmp.x == val[i])
					continue;
				int j = e[tmp.x][i].x;
				if (d[j] > e[tmp.x][i].w + tmp.w) {
					d[j] = tmp.w + e[tmp.x][i].w;
					pq.push({j, d[j]});
				}
			}
		}
		ans = min(ans, d[n]);
	}
	printf("%.2lf", ans);
	return 0;
}

2024/7/17 11:34
加载中...