HDU4081 Qin Shi Huang's National Road System

#include  using namespace std; const int MAXN = 1100; const int INF = 147483647; bool vis[MAXN]; double lowc[MAXN]; int pre[MAXN]; double MAX[MAXN][MAXN]; bool used[MAXN][MAXN]; double x[1010], y[1010], people[1010]; double cost[MAXN][MAXN]; void init() { memset(lowc, 0, sizeof(lowc)); memset(pre, 0, sizeof(pre)); memset(cost, 0, sizeof(cost)); } double Prim(int n) { double ans = 0; memset(vis, false, sizeof(vis)); memset(MAX, 0, sizeof(MAX)); memset(used, false, sizeof(used)); vis[0] = true; pre[0] = -1; lowc[0] = 0; for (int i = 1; i < n; i++) { lowc[i] = cost[0][i]; pre[i] = 0; } for (int i = 1; i < n; i++) { double minc = INF; int p = -1; for (int j = 0; j < n; j++) { if (!vis[j] && minc > lowc[j]) { minc = lowc[j]; p = j; } } if (minc == INF) { return -1; } ans += minc; vis[p] = true; used[p][pre[p]] = used[pre[p]][p] = true; for (int j = 0; j < n; j++) { if (vis[j]&&j!=p) {//这里加上j!=p就AC了,目前原因未知 MAX[j][p] = MAX[p][j] = max(MAX[j][pre[p]], lowc[p]); } if (!vis[j] && lowc[j] > cost[p][j]) { lowc[j] = cost[p][j]; pre[j] = p; } } } return ans; } double dist(double xa, double ya, double xb, double yb) { return sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb)); } int main() { int _, n; scanf("%d", &_); while (_--) { scanf("%d", &n); init(); for (int i = 0; i < n; i++) { scanf("%lf %lf %lf", &x[i], &y[i], &people[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cost[i][j] = cost[j][i] = dist(x[i], y[i], x[j], y[j]); } } double sum = Prim(n), ans = -1; // cout << sum << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (!used[i][j]) { ans = max(ans, (people[i] + people[j]) / (sum - MAX[i][j])); } else { ans = max(ans, (people[i] + people[j]) / (sum - cost[i][j])); } } } printf("%.2f\n", ans); } return 0; }

你可能感兴趣的:(HDU4081 Qin Shi Huang's National Road System)