Why MLE
查看原帖
Why MLE
608410
封禁用户楼主2024/10/16 17:59

RT,70pts.不会是优先队列炸了吧

#include<bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 5;
const int maxm = 5000 * 5000 + 5;

int n, tot, hd[maxn], nxt[maxm], to[maxm], x[maxn], y[maxn];
double ans, val[maxm];

void add(int u, int v, double c) {
	tot++;
	nxt[tot] = hd[u];
	hd[u] = tot;
	to[tot] = v;
	val[tot] = c;
}

double Dis(int a, int b) {
	return sqrt(1.00 * pow(abs(y[a] - y[b]), 2) + pow(abs(x[a] - x[b]), 2));
}

inline int read() {
	int ret = 0, f = 1;char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -f;
		ch = getchar();
	} 
	while (ch >= '0' && ch <= '9') {
		ret = (ret << 3) + (ret << 1) + (ch ^ 48);
		ch = getchar();
	}
	return ret * f;
}

struct node {
	int s;
	double w;
	bool operator < (const node & x) const {
		return w > x.w;
	}
};

priority_queue<node> q;
double dis[maxn];
bool vis[maxn];
void prim() {
	int cnt = 0;
	memset(dis, 0x7f, sizeof(dis));
	dis[1] = 0;
	q.push({1, 0});
	while (!q.empty()) {
		if (cnt == n) break; 
		node now = q.top();q.pop();
		int S = now.s;double d = now.w;
		if (vis[S]) continue;
		vis[S] = 1;
		ans += d;
		cnt++; 
		for (int i = hd[S];i;i = nxt[i]) {
			double Val = val[i];int v = to[i];
			if (Val < dis[v]) {
				dis[v] = Val;
				q.push({v, dis[v]});
			} 
		} 
	}
}

int main() {
	n = read();
	for (int i = 1;i <= n;i++)  x[i] = read(), y[i] = read();
	
	for (int i = 1;i < n;i++) {
		for (int j = i + 1;j <= n;j++) {
			double dis = Dis(i, j);
			add(i, j, dis), add(j, i, dis);
		}
	}
	
//	for (int i = 1;i <= n;i++) {
//		for (int j = hd[i];j;j = nxt[j]) {
//			cout << i << " " << to[j] << ' ' << val[j] << '\n';
//		}
//	}
	
	prim();
	
	printf("%.2lf", ans);
}
2024/10/16 17:59
加载中...