您的位置:首页 > 理论基础 > 计算机网络

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

2015-07-25 16:27 507 查看
http://poj.org/problem?id=1679

The Unique MST

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 23339Accepted: 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

, Max

;//maps[i][j]线段线段ij的花费,Max记录树外最大的线段的花费
int dist
, f
, n;//f[i]  i的父节点即将点i连入树的起点,dist[i]将i连入树需要的花费
bool vis
, use

;//vis[i]标记点i是否在树种,use[i][j]标记线段ij是否在树中

void Init()//初始化
{
memset(vis, false, sizeof(vis));
memset(use, false, sizeof(use));
memset(dist, 0, sizeof(dist));
memset(f, 0, sizeof(f));
int i, j;
for(i = 0 ; i < N ; i++)
{
for(j = 0 ; j < N ; j++)
{
if(i == j)
maps[i][j] = 0;
else
maps[i][j] = INF;
}
}
}

int prim(int s)//求最小生成树
{
int index, Min, i, j, ans = 0;
for(i = 1 ; i <= n ; i++)
{
dist[i] = maps[s][i];
f[i] = s;
}
vis[s] = true;
for(i = 1 ; i < n ; i++)
{
Min = INF;
for(j = 1 ; 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 = 1 ; 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 = 1 ; i < n ; i++)
{
for(j = i + 1 ; 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(1);
num2 = SMST(num1);
if(num1 == num2)//最小生成树与次小生成树相等,则最小生成树不唯一
printf("Not Unique!\n");
else
printf("%d\n", num1);
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: