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 ...
随机推荐
- gitolite 丢失管理密钥/访问权限 解决办法
登录到服务器. 使用完整路径克隆管理员仓库: git clone $HOME/repositories/gitolite-admin.git temp cd gitolite-admin/conf v ...
- Python变量常量及注释
一.变量命名规则1.有字母.数字.下划线搭配组合而成2.不能以数字开头,更不能全为数字3.不能用Python的关键字4.不要太长5.名字要有意义6.不要用中文7.区分大小写8.采用驼峰体命名(多个单词 ...
- 【转载】android 常用开源框架
对于Android初学者以及对于我们菜鸟,这些大神们开发的轻量级框架非常有用(更别说开源的了). 下面转载这10个框架的介绍:(按顺序来吧没有什么排名). 一. Afinal 官方介绍: Afina ...
- HTML5 canvas制作童年的回忆大风车
今天看到一篇CSS3写的大风车http://www.cnblogs.com/yaojaa/archive/2013/01/30/2882521.html,感觉CSS3太神奇了,这在以前用CSS是想都不 ...
- HTML5 Geolocation位置信息定位总结
现在定位功能很常用,所以抽出一些时间将这个功能的知识总结一下作为知识梳理的依据.HTML5 Geolocation的定位用法很简单,首先请求位置信息,用户同意,则返回位置信息.HTML5 Geoloc ...
- 11.24Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.990 测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.991 测试 王宇杰 负责后台代码测 ...
- object-oriented 第二次作业(2)
面向对象程序设计自学计划 由于我的英文实在是很差,所以我就没有去考虑看英文的课程视频.网络上的课程有很多,什么学校的也有,一开始我不知道该如何开始选择课程.感觉每个都还可以.后来在群里的看到别人推荐的 ...
- 【IdentityServer4文档】- 支持和咨询选项
支持和咨询选项 我们为 IdentityServer 提供多个免费和商业支持及咨询选项. 免费支持 免费支持是基于社区的,而且使用的是公共论坛 StackOverflow 有越来越多的使用 Ident ...
- freefcw/hustoj Install Guide
First of all, this version hustoj is a skin and improved for https://code.google.com/p/hustoj/. So t ...
- OSPF学习中的问题
OSPF对接两方,对设置的要求,哪些参数必须相同 (HELLO &dead interval, area ID, authentation, 末节区域(option中的E位), network ...