poj 1679 http://poj.org/problem?id=1679

http://poj.org/problem?id=1679

The Unique MST
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 23339   Accepted: 8284

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>
#include<ctype.h>
#include<stdlib.h>
#define INF 0x3f3f3f3f
#define max(a, b)(a > b ? a : b)
#define min(a, b)(a < b ? a : b)
#define N 110 int maps[N][N], Max[N][N];//maps[i][j]线段线段ij的花费,Max记录树外最大的线段的花费
int dist[N], f[N], n;//f[i] i的父节点即将点i连入树的起点,dist[i]将i连入树需要的花费
bool vis[N], use[N][N];//vis[i]标记点i是否在树种,use[i][j]标记线段ij是否在树中 void Init()//初始化
{
memset(vis, false, sizeof(vis));
memset(use, false, sizeof(use));
memset(dist, , sizeof(dist));
memset(f, , sizeof(f));
int i, j;
for(i = ; i < N ; i++)
{
for(j = ; j < N ; j++)
{
if(i == j)
maps[i][j] = ;
else
maps[i][j] = INF;
}
}
} int prim(int s)//求最小生成树
{
int index, Min, i, j, ans = ;
for(i = ; i <= n ; i++)
{
dist[i] = maps[s][i];
f[i] = s;
}
vis[s] = true;
for(i = ; i < n ; i++)
{
Min = INF;
for(j = ; j <= n ; j++)
{
if(!vis[j] && dist[j] < Min)
{
Min = dist[j];
index = j;
}
}
vis[index] = true;
ans += Min;
use[f[index]][index] = use[index][f[index]] = true;
for(j = ; j <= n ; j++)
{
if(vis[j] && index != j)
Max[index][j] = Max[j][index] = max(Max[f[index]][j], maps[f[index]][index]);
if(!vis[j] && dist[j] > maps[index][j])
{
dist[j] = maps[index][j];
f[j] = index;
}
}
}
return ans;
} int SMST(int num)//求次小生成树
{
int i, j, Min = INF;
for(i = ; i < n ; i++)
{
for(j = i + ; j <= n ; j++)
{
if(!use[i][j] && maps[i][j] != INF)
Min = min(Min, num + maps[i][j] - Max[i][j]);
}
}
return Min;
} int main()
{
int t, m, x, y, w, num1, num2;
scanf("%d", &t);
while(t--)
{
Init();
scanf("%d%d", &n, &m);
while(m--)
{
scanf("%d%d%d", &x, &y, &w);
maps[x][y] = maps[y][x] = w;
}
num1 = prim();
num2 = SMST(num1);
if(num1 == num2)//最小生成树与次小生成树相等,则最小生成树不唯一
printf("Not Unique!\n");
else
printf("%d\n", num1);
}
return ;
}
上一篇:线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468


下一篇:jspace2d——A free 2d multiplayer space shooter