UVA10462:Is There A Second Way Left? (判断次小生成树)
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? (判断次小生成树)的更多相关文章
- UVA-10462.Is There A Second Way Left(Kruskal+次小生成树)
题目链接 本题大意:这道题用Kruskal较为容易 参考代码: #include <cstdio> #include <cstring> #include <vector ...
- poj 1679 判断MST是不是唯一的 (次小生成树)
判断MST是不是唯一的 如果是唯一的 就输出最小的权值和 如果不是唯一的 就输出Not Unique! 次小生成树就是第二小生成树 如果次小生成树的权值和MST相等 那么MST就不是唯一的 法一: ...
- POJ 1679 The Unique MST (次小生成树 判断最小生成树是否唯一)
题目链接 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. De ...
- 次小生成树 判断 unique MST
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- POJ-1679 The Unique MST(次小生成树、判断最小生成树是否唯一)
http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum s ...
- nyoj_118:修路方案(次小生成树)
题目链接 题意,判断次小生成树与最小生成树的权值和是否相等. 豆丁文档-- A-star和第k短路和次小生成树和Yen和MPS寻路算法 法一: 先求一次最小生成树,将这棵树上的边加入一个向量中,再判断 ...
- POJ 1679 The Unique MST(最小生成树)
Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...
- The Unique MST (判断是否存在多个最小生成树)
The Unique MST Time Limit: 10 ...
- POJ 1679 The Unique MST(判断最小生成树是否唯一)
题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its min ...
随机推荐
- 【WXS数据类型】Function
属性: 名称 值类型 说明 [Function].constructor [String] 返回值为“Function”,表示类型的结构字符串 [Function].length [Number] 返 ...
- 原生js实现轮播图原理
轮播图的原理1.图片移动实现原理:利用浮动将所有所有照片依次排成一行,给这一长串图片添加一个父级的遮罩,每次只显示一张图,其余的都隐藏起来.对图片添加绝对定位,通过控制left属性,实现照片的移动. ...
- 【RandomString】- 随机字符串
RandomString 随机字符串的用法
- java 流 文件 IO
Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...
- python 文件编译成exe可执行文件。
pyinstaller打包方法: pyinstaller安装参考地址:http://www.pyinstaller.org/ pywin32的下载地址:https://sourceforge.net/ ...
- 国内版Office365实现MFA的方案(未完)
现在二十一世纪互联版也可以实现了MFA,现在也就是2017年3月份,支持了PC,但是对移动端应用还是不支持的,请了解. 具体方法如下: 登录国内版Office365(事例为高级商业版 https:// ...
- LogisticRegression Algorithm——机器学习(西瓜书)读书笔记
import numpy as np from sklearn.datasets import load_breast_cancer import sklearn.linear_model from ...
- SpringBoot在IDEA下使用JPA
1依赖 使用IDEA构建基于JPA的项目需要引用JPA.MYSQL依赖 2配置文件修改 2.1连接库 spring.datasource.url=jdbc:mysql://localhost:3306 ...
- BAT 批处理脚本 教程 【转】
BAT 批处理脚本 教程 第一章 批处理基础 第一节 常用批处理内部命令简介 批处理定义:顾名思义,批处理文件是将一系列命令按一定的顺序集合为一个可执行的文本文件,其扩展名为BAT或者CMD.这些命令 ...
- Fluent Python: @property
Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...