Is There A Second Way Left?

Description:

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

题意:

看看这个输出就差不多知道了。。先看最小生成树是否存在,然后看次小生成树,如果存在,输出次小生成树的值。

题解:

基本上是模板题,直接看代码吧...

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
int t,n,m;
int flag1;
struct Edge{
int u,v,w;
bool operator < (const Edge &A)const{
return w<A.w;
}
}e[N];
int f[N],mp[N][N];
int d[N][N],dis[N][N];
int check[N],vis[N],link[N][N];
int find(int x){
return f[x]==x?f[x]:f[x]=find(f[x]);
}
ll Kruskal(){
ll ans=,cnt=;
for(int i=;i<=n+;i++) f[i]=i;
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v;
int fx=find(u),fy=find(v);
if(fx==fy) continue ;
f[fx]=fy;
vis[i]=;
cnt++;
mp[u][v]=mp[v][u]=;
link[u][v]=;
ans+=e[i].w;
dis[u][v]=e[i].w;
}
if(cnt!=n-) flag1=;
return ans ;
}
void dfs(int u,int fa){
for(int i=;i<=n;i++){
if(check[i]){
if(link[u][fa]) d[i][u]=d[u][i]=max(d[i][fa],dis[u][fa]);
else d[i][u]=d[u][i]=max(d[i][fa],dis[fa][u]);
}
}
check[u]=;
for(int i=;i<=n;i++){
if(mp[u][i] && i!=fa) dfs(i,u);
}
}
int main(){
scanf("%d",&t);
int cnt = ;
while(t--){
cnt++;
scanf("%d%d",&n,&m);
flag1=;
memset(dis,,sizeof(dis));
memset(mp,,sizeof(mp));
for(int i=;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[i]=Edge{u,v,w};
}
sort(e+,e+m+);
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
memset(link,,sizeof(link));
memset(check,,sizeof(check));
ll sum = Kruskal();
printf("Case #%d : ",cnt);
if(flag1){
puts("No way");
continue ;
}
if(m==n-){
puts("No second way");
continue ;
}
dfs(,-);
ll ans=INF;
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v,w=e[i].w;
if(vis[i]) continue ;
ans=min(ans,sum-d[u][v]+w);
}
cout<<ans<<endl;
}
return ;
}

UVA10462:Is There A Second Way Left? (判断次小生成树)的更多相关文章

  1. UVA-10462.Is There A Second Way Left(Kruskal+次小生成树)

    题目链接 本题大意:这道题用Kruskal较为容易 参考代码: #include <cstdio> #include <cstring> #include <vector ...

  2. poj 1679 判断MST是不是唯一的 (次小生成树)

    判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树  如果次小生成树的权值和MST相等  那么MST就不是唯一的 法一: ...

  3. POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)

    题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...

  4. 次小生成树 判断 unique MST

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  5. POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)

    http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...

  6. nyoj_118:修路方案(次小生成树)

    题目链接 题意,判断次小生成树与最小生成树的权值和是否相等. 豆丁文档-- A-star和第k短路和次小生成树和Yen和MPS寻路算法 法一: 先求一次最小生成树,将这棵树上的边加入一个向量中,再判断 ...

  7. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  8. The Unique MST (判断是否存在多个最小生成树)

    The Unique MST                                                                        Time Limit: 10 ...

  9. POJ 1679 The Unique MST(判断最小生成树是否唯一)

    题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...

随机推荐

  1. spring boot 报错 Error creating bean with name

    Application 启动类 要和父目录平级

  2. 下落的树叶 (The Falling Leaves UVA - 699)

    题目描述: 原题:https://vjudge.net/problem/UVA-699 题目思路: 1.依旧二叉树的DFS 2.建树过程中开个数组统计 //紫书源代码WA AC代码: #include ...

  3. 为什么安装beego和框架的失败 以及常用命令

    1.安装了几个版本,版本之间相互影响. 把没用的删掉 2.网上找的教程存在问题. 都是相互抄袭.最权威的还是官网. which go rm -rf test/ echo path 获取路径 vim ~ ...

  4. Juice账号

    zhangxiaocong69 zxc6545398 15657167502 区块链账户: 0x00680404766965143796a0a070835c3cdf9a4a50

  5. php性能优化--opcache

    一.OPcache是什么? OPcache通过将 PHP 脚本预编译的字节码存储到共享内存中来提升 PHP 的性能, 存储预编译字节码的好处就是 省去了每次加载和解析 PHP 脚本的开销. PHP 5 ...

  6. 关于ES6-{块级作用域 let const 解构赋值 数组 字符串 函数的扩展 箭头函数}

    关于ES6 块级作用域 任何一对花括号({})中的语句集都属于一个块,在块中声明的变量在代码块外都是不可访问的,称之为块级作用域,ES5以前没有块级作用域 let let 是ES6新增的声明变量的一种 ...

  7. jspSmartUpload上传下载使用例子

    --------------------------------------------------------------------- ServletUpload.java 上传 package ...

  8. PCA算法理解及代码实现

    github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维   在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...

  9. Sparsity Invariant CNNs

    文章链接 Abstract 本文研究稀疏输入下的卷积神经网络,并将其应用于稀疏的激光扫描数据的深度信息完成实验.首先,我们表明,即使当丢失数据的位置提供给网络时,传统卷积网络在应用于稀疏数据时性能也很 ...

  10. lintcode-153-数字组合 II

    153-数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素 ...