The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35193 | Accepted: 12860 |
Description
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.
Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
Source
POJ Monthly--2004.06.27 srbga@POJ
大体意思就是建一个生成树,让其中一个边消耗为0后,求(边两边的城市人口数量总和)/ 生成树总消耗 的最大值;
这样的话,我们就可以先建出最小生成树,然后枚举每一个边,假如这条边在最小生成树中用到了,那么生成树的最小消耗就是mst-这条边的消耗,如果没有被用到,那么和最小生成树放在一起之后就会形成一个环,那么最小消耗就是mst-这个环里的最长边;
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
double dis[1005];
double Max[1005][1005];
int pre[1005];
double Map[1005][1005];
bool use[1005][1005];
bool vis[1005];
int v[1005];
struct ***
{
int x, y;
}loca[1005];
int n, m;
double cal(int x, int y)
{
double len= sqrt((loca[x].x - loca[y].x)*(loca[x].x - loca[y].x) + (loca[x].y - loca[y].y)*(loca[x].y - loca[y].y));
return len;
}
double prim()
{
double ans = 0;
memset(use, 0, sizeof(use));
memset(vis, 0, sizeof(vis));
memset(Max, 0, sizeof(Max));
for (int s = 2; s <= n; s++)
{
dis[s] = Map[1][s];
pre[s] = 1;
}
vis[1] = 1;
pre[1] = 0;
dis[1] = 0;
for (int s = 2; s <= n; s++)
{
double min_ans = inf*1.0;
int k;
for (int s = 1; s <= n; s++)
{
if (!vis[s] && min_ans > dis[s])
{
min_ans = dis[s];
k = s;
}
}
if (min_ans == inf)return -1;
ans += min_ans;
vis[k] = 1;
use[k][pre[k]] = use[pre[k]][k] = 1;
for (int s = 1; s <= n; s++)
{
if (vis[s] && s != k)
Max[s][k] = Max[k][s] = max(Max[s][pre[k]], dis[k]);
if (!vis[s]&&dis[s]>Map[k][s])
{
dis[s] = Map[k][s];
pre[s] = k;
}
}
}
return ans;
}
int main()
{
int te;
scanf("%d", &te);
while (te--)
{
scanf("%d", &n);
for (int s = 1; s <= n; s++)
scanf("%d%d%d", &loca[s].x, &loca[s].y, &v[s]);
for (int s = 1; s < n; s++)
for (int w = s + 1; w <= n; w++)
Map[s][w] = Map[w][s] = cal(s, w);
double mst = prim();
double ans = 0;
for (int s = 1; s < n; s++)
{
for (int w = s + 1; w <= n; w++)
{
if (use[s][w])
{
ans = max(ans, (v[s] + v[w])*1.0 / (mst - Map[s][w]));
}
else
{
ans = max(ans, (v[s] + v[w])*1.0 / (mst - Max[s][w]));
}
}
}
printf("%.2lf\n", ans);
}
}