poj 1679 http://poj.org/problem?id=1679
2015-07-25 16:27
507 查看
http://poj.org/problem?id=1679
The Unique MST
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
Sample Output
The Unique MST
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23339 | Accepted: 8284 |
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; }
相关文章推荐
- [网络流24题] 03 最小路径覆盖问题(有向无环图最小路径覆盖,网络最大流)
- ubuntu设置系统时间与网络时间同步(转)
- ubuntu设置系统时间与网络时间同步(转)
- tcp/ip学习笔记
- 黑马程序员——JAVA基础——网络编程
- 安卓检测wifi网络状态以及强度,及检测电量。
- poj 1459 Power Network 网络流 ek算法
- 分布式网络爬虫Nutch中文教程nutcher(JAVA)
- 完美网络
- Unity基于TCP/IP的小聊天室实现
- keepalived+httpd+tomcat实现高可用负载均衡
- HDU1565 方格取数(1) 网络流
- python基础教程总结13——网络编程,
- http协议的一些基础介绍
- iOS- 网络访问JSON数据类型与XML数据类型的实现思路及它们之间的区别
- vsftpd安装与配置--研究tcp与防火墙
- HttpURLConnection 下载
- 使用TCP/IP的套接字(Socket)进行通信
- nyoj170 网络的可靠性(第三届河南省程序设计大赛)
- 没有任何关闭socket的日志,客户端和服务端进程都在, 网络连接完好, 为什么进行某操作后好好的tcp连接莫名其妙地断了呢?