Poj(3522),UVa(1395),枚举生成树】的更多相关文章

题目大意:给出一个n个节点的图,求最大边权值减去最小边权值最小的生成树. 题解 Flash Hu大佬一如既往地强 先把边从小到大排序 然后依次加入每一条边 如果已经连通就把路径上权值最小的边删去 然后记得更新答案 ps:不是很明白为啥我洛谷上吸了氧还跑得更慢了233 //minamoto #include<cstdio> #include<algorithm> #include<iostream> #define inf 0x3f3f3f3f using namespa…
Description The main land of Japan called Honshu is an island surrounded by the sea. In such an island, it is natural to ask a question: “Where is the most distant point from the sea?” The answer to this question for Honshu was found in 1996. The mos…
题目链接:http://poj.org/problem?id=3522 Slim Span Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7522   Accepted: 3988 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G…
Slim Span Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3522 Description Given an undirected weighted graph G, you should find one of spanning trees specified as follows. The graph G is an ordered pair (V, E), where V is a se…
http://poj.org/problem?id=3522 一开始做这个题的时候,以为复杂度最多是O(m)左右,然后一直不会.最后居然用了一个近似O(m^2)的62ms过了. 一开始想到排序,然后扫一个长度n - 1区间,要快速判定这个区间能否构成MST,一直都想不到优秀的算法,然后干脆暴力了. 两种方法,1.dfs,删边容易,标记一下就好,但是这是不行的,删边确实容易,但是dfs的时候多次访问无用的边,所以TLE了. 2.并查集,这个复杂度是O(n)的,能AC,但是我的思路还是有一点bug,…
https://vjudge.net/problem/UVA-1395 题意: 给出一个n结点的图,求苗条度(最大边减最小边的值)尽量小的生成树. 思路: 主要还是克鲁斯卡尔算法,先仍是按权值排序,对于一个连续的边集区间[L,R],如果这些边使得n个点全部连通,则一定存在一个苗条度不超过W[R]-W[L]的生成树.从小到大枚举L,对于每个L,从小到大枚举R. 这道题目我一直超时,最后发现数组开小了,我一直以为数组开小了肯定会出来Runtime error的... #include<iostrea…
题意: 求出最小生成树中最大边与最小边差距的最小值. 分析: 排序,枚举最小边, 用最小边构造最小生成树, 没法构造了就退出 #include <stdio.h> #include <string.h> #include <iostream> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <se…
思路:枚举所有可能的情况. 枚举最小边, 然后不断加边, 直到联通后, 这个时候有一个生成树.这个时候,在目前这个最小边的情况可以不往后枚举了, 可以直接更新答案后break. 因为题目求最大边减最小边最小, 在最小边确定的情况下, 要使得差值最小, 就要使得最大边最小, 那么排序之后加边后 的第一个生成树一定是此情况下的最优解, 因为这个时候最大边最小, 后面的边肯定更大. 细节(1)注意题目给的点标号从1开始还是从0开始.(2)边数组可以用vector(3)find函数最后 return f…
题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king or…
题意:       让你求一颗生成树,使得最长边和最短边长度差值最小. 思路:      额!!!感觉这个思路会超时,但是ac了,暂时没什么别的好思路,那么就先说下这个思路,大牛要是有好的思路希望能在下面留言,相互学习,我的方法是先把所有的边都按长度排序,然后枚举没一颗生成树,这样枚举能得到正确答案的原因是,如果是求最小的差值,那么最终的答案一定是在sort之后的连续的以段,我们只要枚举每一段就行了,但是这样的时间复杂度是O(M^2)的,如果碰到奇葩数据估计一组可能跑到将近1s这样就T了,呵呵.…