poj 1679 http://poj.org/problem?id=1679
http://poj.org/problem?id=1679
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23339 | Accepted: 8284 |
Description
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
Output
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的更多相关文章
- poj 1651 http://poj.org/problem?id=1651
http://poj.org/problem?id=1651Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K To ...
- poj-3056 http://poj.org/problem?id=3056
http://poj.org/problem?id=3056 The Bavarian Beer Party Time Limit: 6000MS Memory Limit: 65536K Tot ...
- POJ3278http://poj.org/problem?id=3278
http://poj.org/problem?id=3278 题目大意: m,n两个数m可+1, -1, *2变成n,需要经过几步 #include<stdio.h> #include&l ...
- OpenJudge/Poj 1207 The 3n + 1 problem
1.链接地址: http://bailian.openjudge.cn/practice/1207/ http://poj.org/problem?id=1207 2.题目: 总时间限制: 1000m ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- poj 1681 Painter's Problem(高斯消元)
id=1681">http://poj.org/problem? id=1681 求最少经过的步数使得输入的矩阵全变为y. 思路:高斯消元求出自由变元.然后枚举自由变元,求出最优值. ...
- POJ 3100 Root of the Problem || 1004 Financial Management 洪水!!!
水两发去建模,晚饭吃跟没吃似的,吃完没感觉啊. ---------------------------分割线"水过....."--------------------------- ...
- <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针
地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展 若满足要求则从左缩减区域 代码如下 正确性调整了几次 然后 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
随机推荐
- JAVA中,不同工程间的方法调用
可以调用, 用配置构建路径的方法:点选工程1, 点击右键, 选择 Build Path(构建路径) - > Configure Build Path...(配置构建路径...)然后在弹出的窗口中 ...
- iOS开发:UINavigationController常用操作
NavigationController常用操作: 更改bar的背景颜色:self.navigationController?.navigationBar.barTintColor =UIColor. ...
- UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!
题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...
- Android中Java反射技术的使用示例
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- [反汇编练习] 160个CrackMe之004
[反汇编练习] 160个CrackMe之004. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...
- error C2471: 无法更新程序数据库 vc90.pdb
error C2471: 无法更新程序数据库“d:/Work/ Project/debug/vc90.pdb” fatal error C1083: 无法打开程序数据库文件:“d:/Work/ Pro ...
- Unit testing Cmockery 简单使用
/********************************************************************** * Unit testing Cmockery 简单使用 ...
- Table '.\mysql\proc' is marked as crashed and should be repaired 报错
Table '.\mysql\proc' is marked as crashed and should be repaired 报错 解决方法: 找到mysql的安装目录的bin/myisamchk ...
- 我的web前端之路 分享些前端的好书(转)
WEB前端研发工程师,在国内算是一个朝阳职业,这个领域没有学校的正规教育,大多数人都是靠自己自学成才.本文主要介绍自己从事web开发以来 (从大二至今)看过的书籍和自己的成长过程,目的是给想了解Jav ...
- HDU 5375 Gray code
题意:给出一个二进制数,其中有些位的数字不确定,对于所有对应的格雷码,与一个序列a对应,第i位数字为1时得分a[i],求最大的得分. 解法:一个二进制数x对应的格雷码为x ^ (x >> ...