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. JAVA基础学习之路(七)对象数组的定义及使用

    两种定义方式: 1.动态初始化: 定义并开辟数组:类名称 对象数组名[] = new 类名称[长度] 分布按成:类名称 对象数组名[] = null: 对象数组名 = new 类名称[长度]:   2 ...

  2. java基础-Comparator接口与Collections实现排序算法

    java 排序Comparable和Comparator使用 java提供了两个排序用的接口Comparable和Comparator,一般情况下使用区别如下: Comparable 接口用于类的固定 ...

  3. UML类图(Class Diagram)中类与类之间的关系及表示方式(转)

    源地址:https://blog.csdn.net/a19881029/article/details/8957441 ======================================== ...

  4. [转载]CENTOS 6.0 iptables 开放端口80 3306 22端口

    原文地址:6.0 iptables 开放端口80 3306 22端口">CENTOS 6.0 iptables 开放端口80 3306 22端口作者:云淡风轻 #/sbin/iptab ...

  5. Centos7添加静态路由

    本文摘取自 Centos7系统配置上的变化(二)网络管理基础 一.ip route显示和设定路由 1.显示路由表 [root@centos7 ~]# ip route show default via ...

  6. 最全的Markdown语法

    目录 Markdown语法 多级标题 引用与注释 插入代码 行内代码 代码段 图片 超链接 行内超链接 参数式超链接 字体 表格 分割线 多级列表 无序列表 有序列表 多选框 LaTeX公式 行内La ...

  7. Paper Reading - Learning like a Child: Fast Novel Visual Concept Learning from Sentence Descriptions of Images ( ICCV 2015 )

    Link of the Paper: https://arxiv.org/pdf/1504.06692.pdf Innovations: The authors propose the Novel V ...

  8. py3.6+anaconda下安装opencv3

    py3.6+anaconda下安装opencv3 首先声明-网上的方法大多数都是有毒的.也不知道给的什么鬼方法都不行. 我说下我的方法.去这个网站https://pypi.tuna.tsinghua. ...

  9. Coursera-Note: Internet History, Technology and Secure (1st week to 9th week)

    目录 Coursera-Note: Internet History, Technology and Secure 第一周 第二周 数据交换: Packet switching技术: 第三周 创造ht ...

  10. OnDraw和Opanit的区别

    OnPaint是WM_PAINT消息的消息处理函数,在OnPaint中调用OnDraw,一般来说,用户自己的绘图代码应放在OnDraw中.  OnPaint() 是CWnd的类成员,负责响应WM_PA ...