Ultraviolet A 5713 Qin Shi Huang's National Road System

Source: Internet
Author: User

Question:

Similar to the Minimum Spanning Tree, the Minimum Spanning Tree is used first, and then vertices are enumerated to delete edges and add edges.

[Cpp]
# Include <cstdio>
# Include <algorithm>
# Include <cstring>
# Include <iostream>
# Include <cmath>
Using namespace std;
Const int maxn = 1010*1010;
Struct NODE {
Int u, v;
Double w;
Bool operator <(const NODE & a) const
{
Return a. w> w;
}
} Node [maxn * 2], in [1005];
 
Struct TREE
{
Int u, next;
Double w;
} Tree [maxn * 5];
Int head [1010];
Bool vis [1010];
Int f [1005];
Double g [1, 1010] [2, 1010];
Int find (int x)
{
Return f [x] = x? X: f [x] = find (f [x]);
}
Int val [1005];
Void dfs (int root, int x)
{
Vis [x] = true;
For (int I = head [x]; I! =-1; I = tree [I]. next)
If (! Vis [tree [I]. u])
{
G [root] [tree [I]. u] = max (tree [I]. w, g [root] [x]);
Dfs (root, tree [I]. u );
}
}
Void kruskal (int n, int e)
{
Memset (head,-1, sizeof (head ));
Double sum = 0, ans = 0;
Int p = 0;
For (int I = 0; I <= n; I ++) f [I] = I;
Sort (node, node + e );
 
For (int I = 0; I <e; I ++)
{
Int x = find (node [I]. u );
Int y = find (node [I]. v );
If (x! = Y)
{
Sum + = node [I]. w;
F [y] = x;
 
// Tree
Tree [p]. u = node [I]. u; tree [p]. w = node [I]. w;
Tree [p]. next = head [node [I]. v]; head [node [I]. v] = p ++;
Tree [p]. u = node [I]. v; tree [p]. w = node [I]. w;
Tree [p]. next = head [node [I]. u]; head [node [I]. u] = p ++;
}
}
 
For (int I = 0; I <= n; I ++)
For (int j = 0; j <= n; j ++)
G [I] [j] = 0;
 
For (int I = 1; I <= n; I ++)
{
For (int j = 0; j <= n; j ++)
Vis [j] = false;
Dfs (I, I );
}
 
// G [I] [j]
 
For (int I = 1; I <= n; I ++)
For (int j = I + 1; j <= n; j ++)
Ans = max (ans, (val [I] + val [j])/(sum-g [I] [j]);
 
Printf ("%. 2f \ n", ans );
}
Int main ()
{
Int ca, n, num;
Int x [1005], y [1005];
Scanf ("% d", & ca );
While (ca --)
{
Scanf ("% d", & n );
Num = 0;
For (int I = 1; I <= n; I ++)
{
Scanf ("% d", & x [I], & y [I], & val [I]);
}
For (int I = 1; I <= n; I ++)
{
For (int j = I + 1; j <= n; j ++)
{
Node [num]. u = I;
Node [num]. v = j;
Node [num]. w = sqrt (x [I]-x [j]) * (x [I]-x [j]) + (y [I]-y [j]) * (y [I]-y [j]);
Num ++;
}
}
Kruskal (n, num );
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.