80分求条,悬一关
查看原帖
80分求条,悬一关
800884
Weekoder楼主2024/3/15 21:53

RT,WA on #3 and #6 QWQ

#include <bits/stdc++.h>
using namespace std;

const int N = 205, M = 10005;

struct node {
	double x, y;
}a[N];
struct nbr_Edge {
	int to;
	double w;
};
struct Edge {
	int u, v;
	double w;
}e[M];
struct queue_element {
	int to;
	double w;
	bool operator<(const queue_element &x)const {
		return w > x.w;
	}
};

int n, m;
double dis1[N], dis2[N], ans = 2147483647;
bool vis[N];

vector<nbr_Edge> nbr[N];

priority_queue<queue_element> pq;

double distance(int X1, int Y1, int X2, int Y2) { return sqrt((X1 - X2) * (X1 - X2) + (Y1 - Y2) * (Y1 - Y2)); }

void dijkstra(int s, double dis[]) {
	for (int i = 1; i <= n; i++) dis[i] = 1 << 30;
	memset(vis, 0, sizeof vis);	
	dis[s] = 0;
	pq.push((queue_element){s, 0});
	while (!pq.empty()) {
		int cur = pq.top().to; pq.pop();
		if (vis[cur]) continue;
		vis[cur] = 1;
		for (auto nxt : nbr[cur]) {
			int to = nxt.to;
			double w = nxt.w;
			if (dis[to] > dis[cur] + w) {
				dis[to] = dis[cur] + w;
				pq.push((queue_element){to, dis[to]});
			}
		}
	} 
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) 
		cin >> a[i].x >> a[i].y;
	for (int i = 1, u, v; i <= m; i++) {
		cin >> e[i].u >> e[i].v;
		e[i].w = distance(a[e[i].u].x, a[e[i].u].y, a[e[i].v].x, a[e[i].v].y);
		nbr[e[i].u].emplace_back((nbr_Edge){e[i].v, e[i].w});
		nbr[e[i].v].emplace_back((nbr_Edge){e[i].u, e[i].w});
	}	
	dijkstra(1, dis1);
	dijkstra(n, dis2);
	for (int i = 1;	i <= m; i++) {
		if (dis1[e[i].u] + e[i].w + dis2[e[i].v] > dis1[n])
			ans = min(ans, dis1[e[i].u] + e[i].w + dis2[e[i].v]);
		if (dis1[e[i].v] + e[i].w + dis2[e[i].u] > dis1[n])
			ans = min(ans, dis1[e[i].v] + e[i].w + dis2[e[i].u]);
	}
	if (ans == 2147483647) cout << -1;
	else printf("%.2lf", ans);
	return 0;
}
2024/3/15 21:53
加载中...