The Unique MST
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!
题意描述:
判断最小生成树是否唯一,在初始最小生成树中去掉一条边让其他边代替,看是否存在能使最小生成树的权值之和不变,若存在则该最小生成树不唯一
程序代码:
#include<stdio.h>
#include<string.h>
#include<math.h>
#define inf 0x7f7f7f7f
struct data{
double x;
double y;
}a[510];
double e[510][510],dis[510],path[510],sum,temp;
int main()
{
int i,j,k,T,m,n,u,min;
int book[510];
scanf("%d",&T);
while(T--)
{
memset(book,0,sizeof(book));
memset(path,0,sizeof(path));
scanf("%d%d",&m,&n);
for(i=1;i<=n;i++)
{
dis[i]=inf;
scanf("%lf%lf",&a[i].x,&a[i].y);
}
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
{
e[i][j]=(double)sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x)+1.0*(a[i].y-a[j].y)*(a[i].y-a[j].y));
e[j][i]=(double)sqrt(1.0*(a[i].x-a[j].x)*(a[i].x-a[j].x)+1.0*(a[i].y-a[j].y)*(a[i].y-a[j].y));
}
// for(i=1;i<=n;i++)
// {
// for(j=1;j<=n;j++)
// printf("%.2f ",e[i][j]);
// printf("\n");
// }
//
//
for(i=1;i<=n;i++)
dis[i]=e[1][i];
// for(i=1;i<=n;i++)
// printf("%.2f",dis[i]);
book[1]=1;
for(k=1;k<=n-1;k++)
{
min=inf;
for(i=1;i<=n;i++)
if(book[i]==0&&dis[i]<min)
{
min=dis[i];
u=i;
}
book[u]=1;
path[u]=dis[u];
for(i=1;i<=n;i++)
if(book[i]==0&&dis[i]>e[u][i])
dis[i]=e[u][i];
}
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(dis[i]>dis[j])
{
temp=dis[i];
dis[i]=dis[j];
dis[j]=temp;
}
printf("%.2f\n",dis[n-m+1]);
}
return 0;
}