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 ...
随机推荐
- git 跟踪分支 远程跟踪分支 学习笔记
远程跟踪分支相当于一个只读仓库指针,从服务器上获取数据,不可以被本地直接修改. 跟踪分支相当于一个本地指针 用于项目更新和迭代. 1跟踪分支 (tracking branch) 逻辑示意图 ...
- Linux文件系统简介和软链接和硬链接的区别
Linux有着极其丰富的文件系统,大体可分为如下几类: 网络文件系统:如nfs.cifs等: 磁盘文件系统:如ext3.ext4等: 特殊文件系统:如prco.sysfs.ramfs.tmpfs等: ...
- maven 安装、配置
简介: maven 大大提高项目开发速度 编译---打包---测试--安装 一条龙 maven将项目构建的过程标准化,每一个阶段使用一个命令完成,下面是构建过程一些阶段 清理 mvn cl ...
- 《javascript模式--by Stoyan Stefanov》书摘--函数
三.函数 1.函数的命名属性 // IE下不支持name属性 var foo = function bar () { // todo }; foo.name; // "bar" 2 ...
- Python中的import语句
Python中的import语句是导入一个文件,这条语句主要做三件事: 1 通过一定的方式,搜寻要导入的文件: 2 如果需要,就编译这个文件: 3 运行这个文件 但是,需要注意的是,所有这三个步骤,都 ...
- 第四课——MFC应用程序框架
一.MFC应用程序类型 上篇文章的彩蛋:可通过使用MFC应用程序向导(MFC AppWizard)的功能来创建所需要的应用程序,这意味着不需要输入任何代码.MFC除了应用程序向导,还对应用程序项目有着 ...
- Easy Summation
Description You are encountered with a traditional problem concerning the sums of powers. Given two ...
- “Hello world!”团队—团队选题展示(视频展示说明)
本次博客的主要内容基本分为以下两方面: 一.视频截图展示 二.视频简要说明 博客内容展示: 视频截图1: 简要说明:这是组长在视频前期简要介绍我们这款游戏项目的内容.从可行性和需求市场方面进行了简要阐 ...
- poj 3009 (深搜求最短路)
题目大意就是求在特定规则下的最短路,这个规则包含了消除障碍的操作.用BFS感觉选择消除障碍的时候不同路径会有影响,用DFS比较方便状态的还原(虽然效率比较低),因此这道题目采用DFS来写. 写的第一次 ...
- sping框架(3)— 使用spring容器
spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.它们都可以代表spring容器,sp ...