UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)
题目大意:
Nasa应邻居们的要求,决定用一个网络把大家链接在一起。给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w。
1:如果不存在最小生成树,输出“No way”.
2:如果不存在次小生成树,输出“No second way”.
3:如果两者都存在,输出次小生成树的长度.
解题思路:
次小生成数+kruskal模板
#include <cmath>
#include <queue>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
const int INF = 0x3f3f3f3f;
const double Exp = 1e-;
struct node
{
int x, y, w; };
bool cmp (node a, node b)
{
return a.w < b.w;
}
node stu[maxn];
int v, e, father[maxn], path[maxn]; void init ()
{
for (int i=; i<=v; i++)
father[i] = i;
} int find (int x)
{
if (father[x] != x)
father[x] = find(father[x]);
return father[x];
}
int kruskal ()
{
int i, j, num = ;
for (i=,j=; i<e; i++)
{
int px = find (stu[i].x);
int py = find (stu[i].y);
if (px != py)
{
num += stu[i].w;
path[j++] = i;
father[px] = py;
}
}
int ans = ;
for (i=; i<=v; i++)
if (father[i] == i)
ans ++;
if (ans == )
return num;
return INF;
}
int smst (int x)
{
int i, num = ;
for (i=; i<e; i++)
{
int px = find(stu[i].x);
int py = find(stu[i].y);
if (px != py && i != x)
{
num += stu[i].w;
father[px] = py;
}
}
int ans = ;
for (i=; i<=v; i++)
if (father[i] == i)
ans ++;
if (ans == )
return num;
return INF;
}
int main ()
{
int t, l = ;
scanf ("%d", &t);
while (t --)
{
scanf ("%d %d", &v, &e);
for (int i=; i<e; i++)
scanf ("%d %d %d", &stu[i].x, &stu[i].y, &stu[i].w);
sort (stu, stu+e, cmp);
int num1 , num2;
num1 = num2 = INF;
init ();
num1 = kruskal();
for (int i=; i<v; i++)
{
init ();
num2 = min (num2, smst(path[i]));
}
if (num1 == INF)
printf ("Case #%d : No way\n", l++);
else if (num2 == INF)
printf ("Case #%d : No second way\n", l++);
else
printf ("Case #%d : %d\n", l++, num2);
}
return ;
}
UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)的更多相关文章
- UVA 10462 Is There A Second Way Left? 次小生成树
模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...
- UVA 10462 —— Is There A Second Way Left?——————【最小生成树、kruskal、重边】
Nasa, being the most talented programmer of his time, can’t think things to be so simple. Recently a ...
- UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树
UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...
- UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解
思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...
- UVA - 10462 Is There A Second Way Left?
题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...
- UVA 10600 ACM Contest and Blackout 次小生成树
又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
- Qin Shi Huang's National Road System UVA - 1494(次小生成树)
秦始皇统一中国之后要在全国修公路连接各个城市,皇帝只想修成最小生成树(距离最小,不考虑人力),一个道士说自己可以不花人力物力修一条路,经过两方妥协,选择max(两个城市人口/(生成树长度-这条路的长度 ...
随机推荐
- C# DataGridView,右键单击RowHeader时显示右键菜单怎么做?
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { ...
- Meteor环境安装配置
在本教程中,我们将展示如何在windows操作系统安装Meteor .在我们开始学习使用Meteor 之前,我们将需要NodeJS.如果你还没有安装它,则可以点击下表中的链接. 必须条件 Meteor ...
- topcoder srm 610
div1 250pt: 题意:100*100的01矩阵,找出来面积最大的“类似国际象棋棋盘”的子矩阵. 解法:枚举矩阵宽(水平方向)的起点和终点,然后利用尺取法来找到每个固定宽度下的最大矩阵,不断更新 ...
- [NPM] Create a new project using the npm init <initializer> command
Historically, the npm init command was solely use to create a new package.json file. However, as of ...
- Deepin-键盘快捷键
是不是很happy呢? 可以用键盘替代鼠标点点点了! 1.鼠标移到右下角 2.下翻找到"快捷键" 3.自定义一个 4.示例(首先编写个简单的Shell) 程序一般放在/usr/bi ...
- libevent多线程使用事项
转 http://www.cnblogs.com/Seapeak/archive/2010/04/08/1707807.html 在linux平台上使用c开发网络程序的同志们一般情况下都对鼎鼎大名的l ...
- Handlebars.js 中文文档
Home » 前端 » Handlebars.js 中文文档 Handlebars.js 中文文档 Posted in 前端 By KeenWon On 2014年4月3日 Views: ...
- 谁是性能杀手?Kafka多Topic下启用SSL时延增大问题分析
问题背景 项目中将Kafka接口进行RESTful封装,在使用RESTful接口进行性能测试时,发现Topic数增多后,开启SSL与非SSL进行测试,发现开启SSL后性能下降得厉害.例如600个Top ...
- mysql 中 unix_timestamp和from_unixtime 时间戳函数
1.unix_timestamp 将时间转化为时间戳.(date 类型数据转换成 timestamp 形式整数) 没传时间参数则取当前时间的时间戳 mysql> select unix_time ...
- Entity Framework工具POCO Code First Generator的使用(参考链接:https://github.com/sjh37/EntityFramework-Reverse-POCO-Code-First-Generator)
在使用Entity Framework过程中,有时需要借助工具生成Code First的代码,而Entity Framework Reverse POCO Code First Generator是一 ...