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. C#非泛型集合和泛型集合

    第一  : ArrayList(非泛型集合)  与List(泛型集合) ArrayList 是数组的复杂版本.ArrayList 类提供在大多数 Collections 类中提供但不在 Array(数 ...

  2. [转]Marshaling a SAFEARRAY of Managed Structures by P/Invoke Part 4.

    1. Introduction. 1.1 In parts 1 through 3 of this series of articles, I have thoroughly discussed th ...

  3. gRPC官方文档(异步基础: C++)

    文章来自gRPC 官方文档中文版 异步基础: C++ 本教程介绍如何使用 C++ 的 gRPC 异步/非阻塞 API 去实现简单的服务器和客户端.假设你已经熟悉实现同步 gRPC 代码,如gRPC 基 ...

  4. Binder学习笔记(三)—— binder客户端是如何组织checkService数据的

    起点从TestClient.cpp的main函数发起: int main() { sp < IServiceManager > sm = defaultServiceManager(); ...

  5. Android为 ContentProvider 提供了那些服务内容?

    ContentProvider 可以调用系统想要共享的内容,安卓为我们提供了哪些和大家共享的东西呢? 官方文档: https://developer.android.com/reference/and ...

  6. oracle如何去除字符串中的重复字符

    create or replace function remove_rame_string(oldStr varchar2, sign varchar2) return varchar2 is /** ...

  7. 787. Cheapest Flights Within K Stops

    There are n cities connected by m flights. Each fight starts from city u and arrives at v with a pri ...

  8. SP839 Optimal marks(最小割)

    SP839 Optimal marks(最小割) 给你一个无向图G(V,E). 每个顶点都有一个int范围内的整数的标记. 不同的顶点可能有相同的标记.对于边(u,v),我们定义Cost(u,v)= ...

  9. 「模拟赛20180406」膜树 prufer编码+概率

    题目描述 给定一个完全图,保证\(w_{u,v}=w_{v,u}\)且\(w_{u,u}=0\),等概率选取一个随机生成树,对于每一对\((u,v)\),求\(dis(u,v)\)的期望值对\(998 ...

  10. 使用between and 作为查询条件