首页 > 其他分享 >The Unique MST(最小生成树的唯一路径)

The Unique MST(最小生成树的唯一路径)

时间:2023-02-14 16:31:51浏览次数:53  
标签:路径 sun MST t2 t3 book 权值 Unique dis


最小生成树唯一的路径就是当前权值里,仅有一条路可以走,不存在最小权值一样的情况,如:1 2 2, 2 3 2, 1 3 2,第一次路径为1—2权值为2,但当下一次到3这个点时就存在分歧,因为1—3的权值是2,2—3的权值也是2,有两个选择。

例题: ​​https://vjudge.net/contest/319242#problem/K​

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!

解题思路:最短路径的prime算法,求出每一次到顶点的权值,然后比较各个点到最短边的权值是否有相同的,注意:会存在至少一条边满足相同,那就是本身这条边。

程序代码:

#include<stdio.h>
#include<string.h>
int e[2000][2000],dis[5000],book[5000];
int inf=99999999;
int main()
{
int i,j,k,n,m,t1,t2,t3,min,l,v,sun;
int count,sum;
int N,flag;
scanf("%d",&N);
while(N--)
{
flag=0;
count=0;
sum=sun=0;
l=1;
memset(dis,0,sizeof(dis));
memset(book,0,sizeof(book));
memset(f,0,sizeof(f));
memset(a,0,sizeof(a));
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(j==i) e[i][j]=0;
else e[i][j]=inf;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
if(e[t1][t2]>t3)//可能同样的边会出现两次,所以取较短的边
{
e[t1][t2]=t3;
e[t2][t1]=t3;
}
}
for(i=1;i<=n;i++)
dis[i]=e[1][i];
book[1]=1;
count++;
while(count<n)
{
min=inf;
for(i=1;i<=n;i++)
{
if(book[i]==0&&dis[i]<min)
{
min=dis[i];
j=i;
}
}
sun=0;//初始化有相同边的数目
for(v=1;v<=n;v++)//寻找n条边中已经标记的的边中是否有相同的边
if(book[v]==1&&min==e[v][j])
sun++;
if(sun>1)//sun>1说明最少有一条边与当前最小值一样
break;
book[j]=1;
sum+=dis[j];
count++;
for(k=1;k<=n;k++)
if(book[k]==0&&dis[k]>e[j][k])
dis[k]=e[j][k];
}
if(sun<=1)
printf("%d\n",sum);
else
printf("Not Unique!\n");
}
return 0;
}


标签:路径,sun,MST,t2,t3,book,权值,Unique,dis
From: https://blog.51cto.com/u_14935708/6057236

相关文章

  • curry MST
    constaddFn=(...args)=>args.reduce((total,cur)=>total+cur,0)constcurry=(fn)=>{letparams=[]returnfunctionf(...rest){i......
  • 7-5 堆中的路径 (25 分)
    7-5 堆中的路径 (25 分)将一系列给定数字插入一个初始为空的小顶堆H[]。随后对任意给定的下标i,打印从H[i]到根结点的路径。输入格式:每组测试第1行包含2个正整数N和......
  • Spring IOC官方文档学习笔记(十)之类路径扫描与组件管理
    1.@Component注解与其衍生注解(1)在Spring中,@Component注解用于说明某个类是一个bean,之后Spring在类路径扫描过程中会将该bean添加至容器中;@Component注解还有很多衍......
  • 找到抓手,用对方法,中电金信关于金融机构数据治理建设路径分享
    前言——在数据管理领域,中电金信是最早一批提供金融业数据管理解决方案的科技公司,见证了金融数据管理的整个发展过程,在二十多年来的实践中沉淀出一套完整的数据治理方法论。......
  • 1138.alphabet-board-path 字母板上的路径
    问题描述1138.字母板上的路径解题思路考虑到'z'单独在一个地方,因此移动顺序中,左下、右上不能反过来,即不能先往下再往左或者先往右再往上。代码classSolution{publi......
  • hamilton路径-图论算法模板
    什么是哈密尔顿路径哈密顿图(哈密尔顿图)(英语:Hamiltoniangraph,或Traceablegraph)是一个无向图,由天文学家哈密顿提出,由指定的起点前往指定的终点,途中经过所有其他节点且只经过......
  • axios的第二种写法,把请求路径抽离到一个文件
    utils/http.jsimportaxiosfrom'axios';importqsfrom'qs';import{VALID_LOGIN}from'_config/url'importcontextfrom'../main.js'importrouterfrom'........
  • Camstar 元数据mdb辅助工具
    1.mdb对象模型介绍定义:CDOs:理解为类(或对象),CDO主要分为Constants(常量,一般在CLF中使用)、Container、Enumeration(枚举)、NamedDataObject(NDO可以直接通过Name操作的对象)、R......
  • Z1932 树上路径
     #Description给你一棵树,树上每个点都有其color现在问你对于每种color,有多少种简单路径经过它#Format##Input一行给出N第二个N个数字,代表每个点的color接下来N-......
  • 使用11G的方式修改12C数据文件路径
    环境:OS:Centos7DB:12.2.0.1从12C之后我们可以使用如下方式在线迁移数据文件alterdatabasemovedatafile'/path/A'to'/path/B'但是使用原11G之前的方法迁移也是......