套的之前的The Unique MST POJ - 1679,,,然后,发现我在更新G的时候,语句不对,应该使用根结点来更新的而不是当前节点,改了以后MAX就更新对了....最后RE了.... 先放着,等以后再找找问题.....
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<map>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 10000+5;
const int INF = 0x3f3f3f3f;
struct Edge{
int u, v;
double dist, cost;
Edge(){
u = 0;
v = 0;
dist = 0;
cost = 0;
};
Edge(int a, int b, double c, double d){
u = a;
v = b;
dist = c;
cost = d;
}
}edge;
struct Pos{
double x, y;
double cost;
}pos[N];
double GetDis(Pos a, Pos b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
vector<int> G[N];
bool operator <(Edge a, Edge b)
{
if(a.dist == b.dist){//加这个,是因为我用了map来标记,,然后,,first是edge,当dist相等时,,他不加入,,所以,这个语句就是为了标记而用
if(a.cost == b.cost){
return a.u > b.u;
}
return a.cost < b.cost;
}
return a.dist > b.dist;
}
ostream & operator<< (ostream & os, const Edge& A){
os << A.u << "--" << A.v << "=" << A.dist << endl;
}
priority_queue<Edge> q;
int judge[N][N] = {0};
double MAX[N][N];
int pre[N] = {0};
void init(int n)
{
// memset(judge, 0, sizeof(judge));
// a.clear();
// cout << a.size() << endl;
// a.erase(a.begin(), a.end());
for(int i=0; i<=n; i++){
pre[i] = i;
G[i].clear();
G[i].push_back(i);
}
}
int Find(int a)
{
return pre[a] == a ? a : pre[a] = Find(pre[a]);
}
void Union(int a, int b)
{
if(a == b){
return;
}
else{
pre[a] = b;
return;
}
}
void Kruskal(map<Edge, int> ta)
{
int sum = 0, u, v;
int Max = 0;
while(!q.empty()){
edge = q.top();
q.pop();
u = Find(edge.u), v = Find(edge.v);
if(u != v){
sum += edge.dist;
ta[edge] = 1;//标记
Union(u, v);
for(int i=0; i<G[u].size(); i++){
for(int j=0; j<G[v].size(); j++){
MAX[G[u][i]][G[v][j]] = MAX[G[v][j]][G[u][i]] = edge.dist;//边是从小到大排序的,直接等于目前最长的边即可
// cout << G[u][i] << "-- " << G[v][j] << "=" << edge.dist<< "-" << edge.cost << endl;
}
}
for(int i=0; i<G[u].size(); i++){
G[v].push_back(G[u][i]);
}
}
}
// cout << sum << endl;
map<Edge, int>::iterator it;
double ans = 0;
for(it=ta.begin(); it!=ta.end(); it++) {
int Ssum = INF;
int u=it->first.u, v=it->first.v;
double cost = it->first.cost, dist = it->first.dist;
// cout << it->first << endl;
// cout<< it->second << "::" << MAX[u][v] << ":" << cost << "/" << sum-MAX[u][v] << "=" << cost/(sum-MAX[u][v]) << endl;
// cout<< cost << "/" << sum-dist << "=" << cost/(sum-dist) << endl;
if(it->second)
ans = max(ans, cost/(sum-dist));
else ans = max(ans, cost/(sum-MAX[u][v]));
}
// cout << sum << ":" << Ssum << endl;
printf("%.2f\n", ans);
}
int main()
{
int T;
cin >> T;
while(T--){
map<Edge, int> a;
int n, m;
cin >> m;
while (!q.empty()) {
q.pop();
}
init(m*m);
for(int i=1; i<=m; i++){
cin >> pos[i].x >> pos[i].y >> pos[i].cost;
}
for (int i = 1; i <= m; i++) {
for(int j=1; j<=m; j++){
if(j == i) continue;
edge.u = i; edge.v = j;
edge.dist = GetDis(pos[i], pos[j]);
// cout << edge.dist << endl;
edge.cost = pos[i].cost+pos[j].cost;
a[edge] = 0;
q.push(edge);
}
}
// cout << a.size() << endl;
Kruskal(a);
}
return 0;
}