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. 哈雷监控设备的操作及升级NSG9k6G

    哈雷监控设备的操作及升级NSG9k6G 一.下载升级包: http://pan.baidu.com/s/1kTmw9sr 如连接不可以用可以直接私聊我.QQ1841031740 二.升级: 下载完后, ...

  2. ubuntu - 14.04,创建菜单

    我们有的时候可能会把一个执行程序放到一个位置,随后我们希望在ubuntu的菜单里面加入它,这个操作非常简单: 我在Gnome桌面里,选择:“系统工具”->“首选项”->"主菜单& ...

  3. Ready api groovy script 参数化

    def token_type =context.expand ('${#Project#token_type}') def access_token = context.expand('${#Proj ...

  4. C++基础学习1: C++布尔类型

    布尔类型(bool)是C++新增的一种基本数据类型.在标准的C语言中并未定义bool类型,如果需要使用bool类型, 程序员可以通过宏定义来自定义一个bool类型,定义语句如下: #define bo ...

  5. 《Professional JavaScript for Web Developers》day03

    <Professional JavaScript for Web Developers>day03 1.1ECMAScript语法 1.1.1 区分大小写 1.1.2 标识符 按照惯例,E ...

  6. Java类成员变量、普通成员变量、初始化块、构造方法的初始化和执行顺序

    结论:执行的大致顺序如下, (1) 在一个不存在继承的类中:初始化static变量,执行static初始化块-->初始化普通成员变量(如果有赋值语句),执行普通初始化块-->构造方法 (2 ...

  7. vue js 实现 树形菜单

    添加一个模板.<template id="menu-template"> <li v-if="model.nodes!=undefined"& ...

  8. Pandas基本功能详解

    Pandas基本功能详解 Pandas  Pandas基本功能详解 |轻松玩转Pandas(2) 参考:Pandas基本功能详解 |轻松玩转Pandas(2)

  9. git 工作中常用命令

    git 命令: git  init  : 初始化 git  add .  :添加所有文件 git  status  :查看状态 若果是第一次会提示你输入你的 邮箱 和姓名: git  commit  ...

  10. 一个基于QT简单登录对话框(带验证码功能)

    1. 对话框样式 2. 源代码 ①. main.cpp #include <QtGui/QApplication> #include "QLoginDialog.h" ...