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

题目大意:给你n个顶点,m条边。如果图是不连通的,输出No way,如果没有次小生成树,输出No second way,如果有次小生成树,输出次小生成树的值。有重边。

解题思路:对于有重边的情况,我们可以用kruskal做,枚举删除最小生成树上的边,进行n-1次枚举,更新出次小生成树。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
struct Edge{
int from,to,dist;
}edges[maxn*maxn];
struct Set{
int pa,rela;
}sets[maxn];
int store[maxn];
bool cmp(Edge a,Edge b){
return a.dist < b.dist;
}
void init(int n){
for(int i = 0; i <= n; i++){
sets[i].pa = i;
}
}
int Find(int x){
if(x == sets[x].pa){
return x;
}
int tmp = sets[x].pa;
sets[x].pa = Find(tmp);
return sets[x].pa;
}
int num =0;
int Kruskal(int n,int m){
init(n);
int rootx,rooty,x,y;
int retsum = 0;
num = 0;
for(int i = 0; i < m; i++){
Edge & e = edges[i];
x = e.from, y = e.to;
rootx = Find(x);
rooty = Find(y);
if(rootx != rooty){
store[num++] = i;
retsum += edges[i].dist;
sets[rooty].pa = rootx;
}
}
if(num < n-1){
return -1;
}else{
return retsum;
}
}
int SecKruskal(int n,int m,int mark){
init(n);
int rootx,rooty,x,y;
int retsum = 0, cnt = 0;
for(int i = 0; i < m; i++){
if(mark == i) continue;
Edge & e = edges[i];
x = e.from, y = e.to;
rootx = Find(x);
rooty = Find(y);
if(rootx != rooty){
cnt++;
retsum += edges[i].dist;
sets[rooty].pa = rootx;
}
}
if(cnt < n-1){
return INF;
}else{
return retsum;
}
}
int main(){
int T,n,m,cas = 0;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int a,b,c;
for(int i = 0; i < m; i++){
scanf("%d%d%d",&a,&b,&c);
a--,b--;
edges[i].from = a;
edges[i].to = b;
edges[i].dist = c;
}
sort(edges,edges+m,cmp);
int mst = Kruskal(n,m);
printf("Case #%d : ",++cas);
if(mst == -1){
puts("No way");
continue;
}
int ans = INF;
for(int i = 0; i < num; i++){
int tmp = SecKruskal(n,m,store[i]);
ans = min(ans,tmp);
}
if(ans == INF){
puts("No second way");
}else{
printf("%d\n",ans);
}
}
return 0;
}

  

UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】的更多相关文章

  1. UVA 10462 Is There A Second Way Left? 次小生成树

    模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...

  2. UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解

    思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...

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

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

  4. UVA - 10462 Is There A Second Way Left?

    题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...

  5. 【UVA 10307 Killing Aliens in Borg Maze】最小生成树, kruscal, bfs

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20846 POJ 3026是同样的题,但是内存要求比较严格,并是没有 ...

  6. 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)

    [题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...

  7. UVA - 1279 Asteroid Rangers (动点的最小生成树)

    题意,有n个匀速动点,求最小生成树的改变次数. 一句话总结:动态问题的一般做法是先求出一个静态的解,然后求出解发生改变的事件,事件按照时间排序,依次处理. 先求出最开始的最小生成树(MST),当MST ...

  8. UVA - 1395 Slim Span (最小生成树Kruskal)

    Kruskal+并查集. 点很少,按边权值排序,枚举枚举L和R,并查集检查连通性.一旦连通,那么更新答案. 判断连通可以O(1),之前O(n)判的,第一次写的过了,后来T.. #include< ...

  9. 【uva 1151】Buy or Build(图论--最小生成树+二进制枚举状态)

    题意:平面上有N个点(1≤N≤1000),若要新建边,费用是2点的欧几里德距离的平方.另外还有Q个套餐,每个套餐里的点互相联通,总费用为Ci.问让所有N个点连通的最小费用.(2组数据的输出之间要求有换 ...

随机推荐

  1. python添加图片验证码

    1.创建验证码模块 #!/usr/bin/env python # -*- coding:utf-8 -*- import random from PIL import Image, ImageDra ...

  2. 前端基础:call,apply,bind的的理解

    背景 前两天在做小程序的需求的时候用到bind的时候才想起自己对这三的东西的了解比较浅薄,这个时候用的时候就有点怕.时候还是要好好学习下,理解下怎么玩. 正文 先说call 和 apply吧:ECMA ...

  3. (原创)E - Straight Shot Gym - 101652R

    解题思路:这道题的题意就是给你n,总距离X,速度v:以及n组数据:人行道的左端点和右端点,以及人行道的速度(竖直方向),如果从(0,0)到(X,0)的时间小于2X/v,则输出其时间,否则输出”Too ...

  4. linux下redis的安装与django-redis使用方法

    Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set( ...

  5. P1919 【模板】A*B Problem升级版(FFT快速傅里叶)

    题目描述 给出两个n位10进制整数x和y,你需要计算x*y. 输入输出格式 输入格式: 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. 输出格式: 输出一 ...

  6. 最短路【洛谷P1462】 通往奥格瑞玛的道路

    P1462 通往奥格瑞玛的道路 题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡 ...

  7. IDEA中jsp页面写out.println会报错?

    解决办法: (1)在WEB-INF目录下新建一个lib目录,并把Tomcat安装目录下的如下两个jar包复制过来. (2)选择jsp-api.jar,右键,Add as Library. (3)在Fi ...

  8. springcloud系列四 搭建服务模块重点讲解

    首先这个服务地址:一定不要写错,是自己注册中心开启的地址 如果注意到这些了,可以简单的进行操作,也可以不需要mybatis与数据库连接,在controller里直接返回相应的数据可以了,不用这么幸苦的 ...

  9. 单片机关键字sfr和sbit的理解

    在单片机C语言编程中,扩充了两个关键字sfr和sbit.sfr(Special Function Register特殊功能寄存器的缩写),sbit(特殊功能寄存器位),与定义一般的int.char型变 ...

  10. hdu-2036求任意多边形面积

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...