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

时间:2023-01-28 16:53:37

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 1679 http://poj.org/problem?id=1679的更多相关文章

  1. poj 1651 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;1651

      http://poj.org/problem?id=1651Multiplication Puzzle   Time Limit: 1000MS   Memory Limit: 65536K To ...

  2. poj-3056 http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;3056

    http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS   Memory Limit: 65536K Tot ...

  3. POJ3278http&colon;&sol;&sol;poj&period;org&sol;problem&quest;id&equals;3278

    http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...

  4. OpenJudge&sol;Poj 1207 The 3n &plus; 1 problem

    1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...

  5. POJ 3320 Jessica&OpenCurlyQuote;s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  6. poj 1681 Painter&amp&semi;&num;39&semi;s Problem&lpar;高斯消元&rpar;

    id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出*变元.然后枚举*变元,求出最优值. ...

  7. POJ 3100 Root of the Problem &vert;&vert; 1004 Financial Management 洪水!!!

    水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...

  8. &lt&semi;挑战程序设计竞赛&gt&semi; poj 3320 Jessica&&num;39&semi;s Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  9. 尺取法 POJ 3320 Jessica&&num;39&semi;s Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath&gt ...

随机推荐

  1. IOS开发基础知识--碎片15

    1:将自定义对象转化成NsData存入数据库 要转为nsdata自定义对象要遵循<NSCoding>的协议,然后实现encodeWithCoder,initwithcode对属性转化,实例 ...

  2. Linux zip&sol;unzip命令

    From: http://www.ixdba.net/a/os/linux/2010/0725/359.html From: http://www.cnblogs.com/chinareny2k/ar ...

  3. 【Android开发学习笔记】【第四课】基础控件的学习

    通过一个简单的例子来学习下面几种控件: 1.TextView:简单的文本显示控件 2.EditText:可以编辑的文本框 3.Button:按钮 4.Menu:这里指的是系统的Menu 5.Toast ...

  4. SendEmail语法

    SendEmail语法 示例: /usr/local/bin/sendEmail -f shengwei.tang@joy4you.com -t @qq.com -s smtp.exmail.qq.c ...

  5. combotree的加载方法

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  6. Rstudio匹配中文

    之前在操作csv文件时一般是将中文字符在excel或用Perl将其中的中文转换成对应的英文,但是最近碰到不得不在R里面进行中文符操作,发现R里面的匹配是无法识别的中文符的.比如: df <- r ...

  7. MFC网页

    写网页, 选择MFC,MFC应用程序,其他默认,单击确定 项目类型,选Offce,其他默认,单击下一步 默认,单击下一步 文件拓展名,输入html,其他默认,单击下一步 数据库支持,默认,单击下一步 ...

  8. VisualVM远程连接Tomcat

    最近项目已经要提测了,有时间来考虑一些性能上的事儿了.之前拜读过<深入理解java虚拟机>,只可惜当时功力尚浅,有些东西还是不太懂,而且应用场景也没有,所以借这次机会看看.当然了,这次并不 ...

  9. B树索引分裂

    一.索引分裂 1.  什么是分裂 在开始介绍之前,我们先来搞清楚什么是索引分裂吧.“索引分裂”就是索引块的分裂,当一次DML事务操作修改了索引块上的数据,但是旧有的索引块没有足够的空间来容纳新修改的数 ...

  10. mysql错误:this authentication plugin is not supported

    this authentication plugin is not supported 应用程序连接mysql docker一直报错:this authentication plugin is not ...