题目链接: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算法的更多相关文章

  1. POJ 1679 The Unique MST (次小生成树kruskal算法)

    The Unique MST 时间限制: 10 Sec  内存限制: 128 MB提交: 25  解决: 10[提交][状态][讨论版] 题目描述 Given a connected undirect ...

  2. UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)

    题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...

  3. HDOJ-4081(次小生成树+Prim算法)

    Qin Shi Huang's National Road System HDOJ-4081 本题考查的是次小生成树的问题,这里的解决方法就是先使用Prim算法求解最小生成树. 在求解最小生成树的时候 ...

  4. POJ-1789 Truck History---最小生成树Prim算法

    题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...

  5. Constructing Roads-最小生成树(kruskal)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题目描述: #include<cstdio> #include<cstring ...

  6. Conscription-最小生成树-Kruskal

    Windy has a country, and he wants to build an army to protect his country. He has picked up N girls ...

  7. 10-最小生成树-Prim算法

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; #def ...

  8. HDU 2988.Dark roads-最小生成树(Kruskal)

    最小生成树: 中文名 最小生成树 外文名 Minimum Spanning Tree,MST 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的 ...

  9. HDU 4081Qin Shi Huang's National Road System(次小生成树)

    题目大意: 有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点.秦始皇希望这所有n-1条路长度之和最短.然后徐福突然有冒出来,说是他有魔法,可以不用人力.财力就变 ...

随机推荐

  1. 【MFC】利用MFC写一个计时器小程序

    1整体设计 创建对话框程序,并且设计对话框相关控件如图 相应的ID和对应的成员变量如图: 我的想法是这样的,只读属性的编辑框添加有CString类型的成员变量(如s_hour),在xxxDlg.h里另 ...

  2. 1. node.js环境搭建 第一行代码

    一.NodeJs简介 NodeJS官网上的介绍: Node.js is a platform built on  Chrome's JavaScript runtime  for easily bui ...

  3. Haproxy配置文件详解

    #/usr/local/sbin/haproxy -f /etc/haproxy/haproxy.cfg -st `cat /var/run/haproxy.pid` ################ ...

  4. 使用inline-box代替float

    在网页布局中,使用float有不少好处,可以为你带来更加自由的布局,还可以自动根据浏览器改变布局效果.但是使用多了你也可能发现有一个问题,使用了float之后,外层的div不会撑高,导致布局出现坍塌. ...

  5. HDU3183A Magic Lamp,和NYOJ最大的数一样

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. NOIP2014D2T2寻找道路(Spfa)

    洛谷传送门 这道题可以把边都反着存一遍,从终点开始深搜,然后把到不了的点 和它们所指向的点都去掉. 最后在剩余的点里跑一遍spfa就可以了. ——代码 #include <cstdio> ...

  7. [转]genymotion Unable to load VirtualBox engine 某种解决办法

    genymotion Unable to load VirtualBox engine 某种解决办法 耳闻genymotion这款模拟器很强力.于是下下来试试看.我的机器上是有virtualbox的了 ...

  8. 跨域访问sessionid不一致问题

    在开发过程中遇到这么一个问题,让我花了一个下午的大好时光才解决.但是解决玩之后,发现那么的容易.就是查找资料的时候很费劲.这里把问题记录一下. 问题的产生 流程是这样的,要做一个用户登录的接口.在登录 ...

  9. free delete malloc new(——高品质量程序设计指南第16章)

    free和delete只是把指针所指向的内存给释放掉了,但是指针本身并没有被删掉. 所以在释放掉内存后一定要记得将指针指向NULL ,动态内存分配不会自动的释放,一定要记得free掉

  10. Mybatis(spring)(多个参数)(插入数据返回id)

    一. 1.两个参数都是int类型() 例子: 1 <  select id="searchClassAllNum" resultType="int"> ...