The Unique MST--hdoj
The Unique MST
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 25 Accepted Submission(s) : 8
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'.
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.
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
3
Not Unique!
#include<stdio.h>
#include<string.h>
#define INF 0xfffffff
int n,m;
int map[1010][1010],dis[1010],mark[1010];
void prim()
{
int i,min,flag,j,sum=0,k,w=1;
memset(mark,0,sizeof(mark));
mark[1]=1;
for(i=2;i<=n;i++)
{
min=INF;
flag=-1;
k=0;
for(j=1;j<=n;j++)
{
if(!mark[j]&&map[1][j]<min)
{
min=map[1][j];
flag=j;
}
}
for(j=1;j<=n;j++)
{
if(mark[j]&&map[flag][j]==min)
{/*因为mark[1]已经标记过了,所以此时如果出现两个及其以上的点,
到flog的距离都是min,当然,仅限于已经连过的点,此时最小生成树就不止一种*/
k++;
}
}
//printf("%d ",k);
if(k>=1)
{
w=0;
break;
}
mark[flag]=1;
sum+=min;
for(j=1;j<=n;j++)
{
if(!mark[j]&&map[1][j]>map[flag][j])
map[1][j]=map[flag][j];
}
}
if(w)
printf("%d\n",sum);
else
printf("Not Unique!\n");
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j;
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
map[i][j]=INF;
//map[i][i]=0;
}
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
map[a][b]=map[b][a]=c;
}
prim();
}
}
The Unique MST--hdoj的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- POJ1679The Unique MST(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25203 Accepted: 8995 D ...
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ 1679 The Unique MST (最小生成树)
The Unique MST 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/J Description Given a conn ...
- poj 1679 The Unique MST(唯一的最小生成树)
http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submis ...
- poj 1679 The Unique MST【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24034 Accepted: 8535 D ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
- POJ 1679:The Unique MST(次小生成树&&Kruskal)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19941 Accepted: 6999 D ...
随机推荐
- OI知识点
- 在命令提示符窗口下(cmd)使用指令操作并编译java代码,运行java编译代码
使用cmd操作java代码,编译.java文件,运行.class文件. 操作步骤: 1:创建一个文件夹: 例如:在e盘根目录(\)下面创建一个名为Hello的文件夹: 使用md指令:如图 在e盘中会生 ...
- Webpack 打包学习
前段时间项目主管让测试组长研究webpack打包方式,闲暇时自己想学习一下,留着备用,本周日学习一下. https://www.jianshu.com/p/42e11515c10f
- DNN结构演进History—CNN( 优化,LeNet, AlexNet )
本文相对于摘抄的文章已经有大量的修改,如有阅读不适,请移步原文. 以下摘抄转自于维基:基于深度学习的图像识别进展百度的若干实践 从没有感知域(receptive field) 的深度神经网络,到固定感 ...
- linux环境jdk安装及配置
linux环境jdk安装及配置 linux环境jdk安装及配置 1.下载jkd( http://www.oracle.com/technetwork/java/javase/downloads/ind ...
- 微信小程序 请求超时处理
1.在app.json加入一句 "networkTimeout": { "request": 10000 } 设置超时时间,单位毫秒 2.请求 wx.reque ...
- 【转】虚拟化(四):vsphere高可用功能前提-共享存储搭建
vsphere高级功能HA.DRS.FT等,都需要有共享存储环境,即多台esxi主机同时连接一个共享存储,这样在新建虚拟机时,可以指定把虚拟磁盘保存在共享存储上,便于虚拟机在各个主机之间“飘移”. 常 ...
- 如何查看Linux的CPU负载
哪些工具可以查看 CPU 负载? 可以使用 top 命令.uptime 命令,特别是 top 命令,功能强大,不仅仅可以用来查看 CPU 负载. CPU 负载怎么理解?是不是 CPU 利用率? 要区别 ...
- 崂山白花蛇草水 权值线段树套KDtree
Description 神犇Aleph在SDOI Round2前立了一个flag:如果进了省队,就现场直播喝崂山白花蛇草水.凭借着神犇Aleph的实 力,他轻松地进了山东省省队,现在便是他履行诺言的时 ...
- swift-UITextfield控件的基本属性设置
//1.初始化UITextField let userName=UITextField(frame: CGRectMake(0, 100, 100, 100)); //2.将文本框userName添加 ...