UVA10462Is There A Second Way Left? —— 次小生成树 kruskal算法
题目链接:https://vjudge.net/problem/UVA-10462
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently all his neighbors have decided to connect themselves over a network (actually all of them want to share a broadband internet connection :-)). But he wants to minimize the total cost of cable required as he is a bit fastidious about the expenditure of the project. For some unknown reasons, he also wants a second way left. I mean, he wants to know the second best cost (if there is any which may be same as the best cost) for the project. I am sure, he is capable of solving the problem. But he is very busy with his private affairs(?) and he will remain so. So, it is your turn to prove yourself a good programmer. Take the challenge (if you are brave enough)...
Input
Input starts with an integer t ≤ 1000 which denotes the number of test cases to handle. Then follows t datasets where every dataset starts with a pair of integers v (1 ≤ v ≤ 100) and e (0 ≤ e ≤ 200). v denotes the number of neighbors and e denotes the number of allowed direct connections among them. The following e lines contain the description of the allowed direct connections where each line is of the form ‘start end cost’, where start and end are the two ends of the connection and cost is the cost for the connection. All connections are bi-directional and there may be multiple connections between two ends.
Output
There may be three cases in the output 1. No way to complete the task, 2. There is only one way to complete the task, 3. There are more than one way. Output ‘No way’ for the first case, ‘No second way’ for the second case and an integer c for the third case where c is the second best cost. Output for a case should start in a new line.
Sample Input 4 5 4 1 2 5 3 2 5 4 2 5 5 4 5 5 3 1 2 5 3 2 5 5 4 5 5 5 1 2 5 3 2 5 4 2 5 5 4 5 4 5 6 1 0
Sample Output
Case #1 : No second way
Case #2 : No way
Case #3 : 21
Case #4 : No second way
题解:
1.求次小生成树。但是题目要求可以有重边,而prim算法处理的是点与点的关系,不能(至少很难)处理有重边的图。
2.求最小生成树,除了prim算法之外,还有kruskal算法,且因为它是直接依据边来进行操作的,所以就能很好地处理重边了。
3.步骤:先用kruskal算法求出最小生成树,同时记录下最小生成树边。然后枚举删除每一条最小生成树边,再去求最小生成树,最终即可得到次小生成树。
4.复杂度分析:O(n*m),计算次数为2e4,再乘上case数,计算次数为2e7,所以可行。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 1e2+; struct Edge
{
int u, v, w;
bool operator<(const Edge &a)const{
return w<a.w;
}
}edge[MAXN<<];
int fa[MAXN], used[MAXN]; int find(int x) { return fa[x]==-?x:x=find(fa[x]); } int kruskal(int n, int m, int pointed)
{
int cnt = , sum = ;
memset(fa, -, sizeof(fa));
for(int i = ; i<=m; i++)
{
if(i==pointed) continue; //如果是那条被指定删除的最小生成树边,则跳过 int u = find(edge[i].u);
int v = find(edge[i].v);
if(u!=v)
{
fa[u] = v;
sum += edge[i].w;
++cnt; //错误:不能放到下一句里面, 即used[++cnt]=i, 因为这条语句必须要执行!!!
if(pointed==-) used[cnt] = i; //如果求最小生成树,则把编号为i的边加入生成树中
if(cnt==n-) return sum; //合并了n-1次,已成树,可直接返回
}
}
return cnt<(n-)?INF:sum; //当合并次数小于n-1时,不能构成树
} int main()
{
int T, n, m;
scanf("%d", &T);
for(int kase = ; kase<=T; kase++)
{
scanf("%d%d",&n,&m);
for(int i = ; i<=m; i++)
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w); sort(edge+, edge++m); int t1 = INF, t2 = INF;
t1 = kruskal(n, m, -); //求最小生成树
for(int i = ; i<=n-; i++) //枚举删除每一条最小生成树边边,求次小生成树
{
int tmp = kruskal(n, m, used[i]);
t2 = min(t2, tmp);
} if(t1==INF) //原图不连通,既没有最小生成树
printf("Case #%d : No way\n", kase);
else if(t2==INF) //没有次小生成树
printf("Case #%d : No second way\n", kase);
else
printf("Case #%d : %d\n", kase, t2);
}
}
UVA10462Is There A Second Way Left? —— 次小生成树 kruskal算法的更多相关文章
- POJ 1679 The Unique MST (次小生成树kruskal算法)
The Unique MST 时间限制: 10 Sec 内存限制: 128 MB提交: 25 解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...
- UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...
- HDOJ-4081(次小生成树+Prim算法)
Qin Shi Huang's National Road System HDOJ-4081 本题考查的是次小生成树的问题,这里的解决方法就是先使用Prim算法求解最小生成树. 在求解最小生成树的时候 ...
- POJ-1789 Truck History---最小生成树Prim算法
题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...
- Constructing Roads-最小生成树(kruskal)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题目描述: #include<cstdio> #include<cstring ...
- Conscription-最小生成树-Kruskal
Windy has a country, and he wants to build an army to protect his country. He has picked up N girls ...
- 10-最小生成树-Prim算法
#include <iostream> #include <cstring> #include <cstdio> using namespace std; #def ...
- HDU 2988.Dark roads-最小生成树(Kruskal)
最小生成树: 中文名 最小生成树 外文名 Minimum Spanning Tree,MST 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的 ...
- HDU 4081Qin Shi Huang's National Road System(次小生成树)
题目大意: 有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点.秦始皇希望这所有n-1条路长度之和最短.然后徐福突然有冒出来,说是他有魔法,可以不用人力.财力就变 ...
随机推荐
- zoj 2724 Windows Message Queue
Windows Message Queue Time Limit: 2 Seconds Memory Limit: 65536 KB Message queue is the basic f ...
- [Vijos1512] SuperBrother打鼹鼠 (二维树状数组)
传送门 直接搞就行. 注意下表re从零开始,而树状数组搞不了0,所以统一增加一个偏移量1. (话说数据随机是什么鬼?) # include <iostream> # include < ...
- SQL Prompt 5.1使用
SQL Prompt 5.1教程 1.下载 自行下载安装文件.本人是从http://www.cr173.com/下载的. 2.安装 安装没什么特别的,不用说了 3.注意一下破解和配置 按里面的read ...
- 最长链(codevs 1814)
题目描述 Description 现给出一棵N个结点二叉树,问这棵二叉树中最长链的长度为多少,保证了1号结点为二叉树的根. 输入描述 Input Description 输入的第1行为包含了一个正整数 ...
- guava缓存设置return null一直报错空指针
guava缓存设置return null一直报错空指针 因为缓存不允许返回为空
- Activiti-5.3工作流引擎-源码解析(流程文档解析)
前面我们通过BPMN20.xsd和Activiti自定义的XML Schema文件初步了解了业务流程模型的定义,那么现在我们来了解一下流程文档的解析过程,这个过程主要是通过代码解析来完成. 代码解析过 ...
- linux定时重启节约内存
linux服务器上运行的一些程序,比较消耗内存,需要定时重启,进行内存定期释放 0 2 * * * sudo /sbin/reboot && echo $(date) '重启成功' ...
- curl 发送json请求
curl 发送json请求 这个是在cmd环境下的输入:注意{\"userName\":\"helo\",\"id\":1}中间不能有空格 ...
- 解决UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordinal not in range(128)的编码问题
当我在运行一个基于scrapy的爬虫时出现UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 12: ordina ...
- PHP根据两点间的经纬度计算距离
/** * 说明: 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ function getDistance($lat ...