您的位置:首页 > 产品设计 > UI/UE

POJ1679The Unique MST(次小生成树)

2015-11-22 17:00 381 查看
The Unique MST
Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 25203Accepted: 8995
DescriptionGiven 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'.
InputThe 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.
OutputFor 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!

题意:问一棵最小生成树是否唯一
找次小生成树,如果相等不唯一,否则唯一

次小生成树:就是最小生成树换一条边而成的生成树;用maxd【x】【y】存储最小生成树两个节点(x,y)路径中最大的那条边的权值,也就是最小生成树中x-z-...-y中那个最大的那一个权值,然后就可以换边了,到底换哪一个边就从不在生成树中的边一个一个枚举咯,假设x-y,如果要用x-y替换x-z...-y肯定得替换x-z...-y中权值最大的那条边才能让最后得到结果只比x-z...-y小一点,也就是仅次于
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAX = 110;
const int INF = 100000000;
int n,m,ans,cnt;
int edge[MAX][MAX],vis[MAX],used[MAX][MAX],dist[MAX];
int pre[MAX],maxd[MAX][MAX];
void prime()
{
memset(vis,0,sizeof(vis));
memset(used,false,sizeof(used));
memset(maxd,0,sizeof(maxd));
for(int i = 1; i <= n; i++)
{
dist[i] = edge[1][i];
pre[i] = 1;
}
dist[1] = 0;
vis[1] = 1;
pre[1] = -1;
for(int i = 1; i < n; i++)
{
int minn = INF,pos = -1;
for(int j = 1; j <= n; j++)
{
if(vis[j] == 0 && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
if(minn == INF)
{
ans = INF;
return;
}
ans += minn;
vis[pos] = 1;
used[pos][pre[pos]] = used[ pre[pos] ][pos] = true;
for(int j = 1; j <= n; j++)
{
if(vis[j])
maxd[j][pos] = maxd[pos][j] = max(dist[pos], maxd[j][pre[pos]]);
if(vis[j] == 0 && dist[j] > edge[pos][j])
{
dist[j] = edge[pos][j];
pre[j] = pos;
}
}
}
}
void smst()
{
cnt = INF;
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
if(used[i][j] == false && edge[i][j] != INF)
{
cnt = min(cnt, ans - maxd[i][j] + edge[i][j]);
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
for(int i = 1; i <= MAX; i++)
for(int j = 0; j <= MAX; j++)   //初始化的时候把MAX,写成了n, orz....
{
if(i != j)
edge[i][j] = INF;
else
edge[i][i] = 0;
}
scanf("%d%d", &n,&m);
for(int i = 0; i < m; i++)
{
int x,y,w;
scanf("%d%d%d", &x, &y, &w);
edge[x][y] = edge[y][x] = w;
}
ans = 0;
prime();
smst();
if(ans == INF)
{
printf("Not Unique!\n");
continue;
}
if(cnt == ans)
{
printf("Not Unique!\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}
View Code

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