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> ...
随机推荐
- SGU 275 To xor or not to xor (高斯消元)
题目链接 题意:有n个数,范围是[0, 10^18],n最大为100,找出若干个数使它们异或的值最大并输出这个最大值. 分析: 一道高斯消元的好题/ 我们把每个数用二进制表示,要使得最后的异或值最大, ...
- POJ 2455 - Secret Milking Machine
原题地址:http://poj.org/problem?id=2455 题目大意:给出一个N个点的无向图,中间有P条边,要求找出从1到n的T条通路,满足它们之间没有公共边,并使得这些通路中经过的最长的 ...
- UVa 1515 (最小割) Pool construction
题意: 输入一个字符矩阵,'.'代表洞,'#'代表草地.可以把草改成洞花费为d,或者把洞改成草花费为f,最后还要在草和洞之间修围栏花费为b. 但要保证最外一圈是草,求最小费用. 分析: 还不是特别理解 ...
- fancybox 点击 js脚本判断验证,fancybox的宽度高度设置
当我们在使用fancybox做弹出窗口的时候,可能在弹窗之前就需要判断一些验证条件,例如我这里有个案例,用户必须先得勾选一个 那么怎么做呢?我们用到fancybox的一个onStart方法就可以了 $ ...
- ECshop 二次开发模板教程4
今天我们学习一下如何在首页调取某个分类的商品:注意了,这里的修改有一些麻烦了哦:首先你需要下载一套新的模板,比如blueksy 上传到模板目录 /themes/ 也就是 /themes/bluesky ...
- mount
产品,平台,RS6000, pseries 软件版本, aix 当NFS在NFS客户端加载时,系统会问是使用 soft-mount 还是hard-mount, 它们之间有什么区别? 它们的区别在于当发 ...
- POJ 1274 The Perfect Stall
题意:有n只牛,m个牛圈(大概是),告诉你每只牛想去哪个牛圈,每个牛只能去一个牛圈,每个牛圈只能装一只牛,问最多能让几只牛有牛圈住. 解法:二分图匹配.匈牙利裸题…… 代码: #include< ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- 有关T-SQL的10个好习惯(转)
1. 在生产环境中不要出现Select * 这一点我想大家已经是比较熟知了,这样的错误相信会犯的人不会太多.但我这里还是要说一下. 不使用Select *的原因主要不是坊间所流传的将*解析成具体的列需 ...
- Filezilla中文字符文件看不到或显示乱码的解决办法
Filezilla确实是跨平台的好软件,可之前我就在ubuntu下郁闷为什么看坛子FTP里竟然是空的.最近换MAC版的FZ结果还是这样就奇怪了. 后来想Filezilla应该是支持字符集转换的,所以在 ...