题目链接: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. Laravel(4.2)-->whereHas/ whereDoesntHave

    在开发过程中,有时间需要用 wherehas 联合查询 出想要的结果,但是有的时候想搜索出不在关联表中出现的数据 whereDoesntHave(例:搜索出开卡的用户和没有开卡的用户)if($is_o ...

  2. 【RMAN】RMAN跨版本恢复(下)--大版本异机恢复

    [RMAN]RMAN跨版本恢复(下)--大版本异机恢复 BLOG文档结构图 ORACLE_SID=ORA1024G 关于10g的跨小版本恢复参考:http://blog.chinaunix.net/u ...

  3. [NOI2003]Editor(块状链表)

    传送门 看了看块状链表,就是数组和链表的合体. 看上去好高大尚,思想也很简单. 但是发现代码量也不是很小,而且代码理解起来也是费尽得很,倒不如splay用起来顺手. 在加上适用范围貌似不是特别广,所以 ...

  4. Codeforces396A - On Number of Decompositions into Multipliers

    Portal Description 给出\(n(n\leq500)\)个\([1,10^9]\)的数,令\(m=\prod_{i=1}^n a_i\).求有多少个有序排列\(\{a_n\}\),使得 ...

  5. ubuntu 18.04取消自动锁屏以及设置键盘快捷锁屏

    1:操作设置取消自动锁屏: setting-->power--->never 2:  设置自动锁屏快捷键: 快捷键设置一般在setting-->devices--->keybo ...

  6. BZOJ1696: [Usaco2007 Feb]Building A New Barn新牛舍

    n<=10000个点(xi,yi),找到一个不同于给出的所有点的点,使得该点到所有点的曼哈顿距离最小并找出这样的点的个数. 第一眼看上去这不是中位数嘛,奇数一个点偶数一片,然后找一下这篇区域有几 ...

  7. Swift--方法(函数)

    方法是执行特殊任务的自包含代码块.你可以给方法名字来表示它的功能,而且在需要的时候调用这个名字的方法来执行它的任务. Swift方法的语法表达很灵活,从类似c的没有参数名的方法到oc复杂的带有名字和参 ...

  8. python之-- 反射

    反射定义:通过字符串映射或者修改程序运行时的状态,属性,方法.方法有如下4个:1:getattr(object,name,default=None):根据字符串去获取obj对象里的对应的方法的内存地址 ...

  9. openstack DVR的AIO 问题

    问题描述 : 创建public 网络,创建路由器,并且把路由器的gateway 设置指向网络后有下面几种错误 路由器对应的linux network namespace 建立起来了,但是里面并没有对应 ...

  10. CentOS系统如何设置服务开机自动运行

    centos安装好apache,mysql等服务器程序后,并没有设置成开机自动启动的,为避免重启后还要手动开启web等服务器,还是做下设置好,其实设置很简单,用chkconfig命令就行了.例如要开机 ...