UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树
题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <cstdlib>
#include <iterator>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <cctype>
using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
}
// #define _DEBUG; //*//
#ifdef _DEBUG
freopen("input", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
/*-----------------show time----------------*/
const int maxn = ;
int n,m;
struct node
{
int u,v;
int c;
bool vis;
}e[maxn]; bool cmp(node a,node b){
return a.c < b.c;
}
int fa[];
int sum,sec_tree;
vector<int>g[];
int len[][];
int find(int x){
if(fa[x]==x)return x;
else return fa[x] = find(fa[x]);
}
void kruskal(){ for(int i=; i<=n; i++){
fa[i] = i;
g[i].clear();
g[i].pb(i);
} sort(e+,e++m,cmp);
sum = ;
int k=;
for(int i=; i<=m; i++){
if(k==n-)break; int fx = find(e[i].u);
int fy = find(e[i].v); if(fx!=fy){
sum += e[i].c; k++;
e[i].vis = true; int len1 = g[fx].size();
int len2 = g[fy].size(); for(int j=; j<len1; j++){
for(int t=; t<len2; t++){
len[g[fx][j]][g[fy][t]] = len[g[fy][t]][g[fx][j]] = e[i].c;
}
} fa[fy] = fx; int tmp[]; for(int j=; j<len1; j++){
tmp[j] = g[fx][j];
}
for(int j=; j<len2; j++){
g[fx].pb(g[fy][j]);
}
for(int j=; j<len1; j++){
g[fy].pb(tmp[j]);
}
}
} sec_tree = inf;
for(int i=; i<=m; i++){
if(e[i].vis==false){
sec_tree = min(sec_tree, sum - len[e[i].u][e[i].v] + e[i].c);
} }
// debug(sec_tree);
}
int main(){
int t_t;
scanf("%d", &t_t);
for(int T = ; T <= t_t;T++){
printf("Case #%d : ", T);
memset(len,,sizeof(len));
scanf("%d%d", &n, &m);
for(int i=; i<=m; i++){
scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].c);
e[i].vis = false;
}
kruskal();
int flag =;
for(int i=; i<=n; i++){
if(find(i) != find()){
puts("No way");
flag = ;
break;
}
}
if(flag == ) continue;
if(sec_tree<inf){
printf("%d\n",sec_tree);
}
else printf("No second way\n");
}
return ;
}
UVA - 10462
UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树的更多相关文章
- 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
[题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...
- UVA 10462 Is There A Second Way Left? 次小生成树
模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...
- UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解
思路: Prim: 这道题目中有重边 Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中) Kruskal: Krus ...
- 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)
题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...
- UVA - 10462 Is There A Second Way Left?
题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...
- UVa 11437:Triangle Fun(计算几何综合应用,求直线交点,向量运算,求三角形面积)
Problem ATriangle Fun Input: Standard Input Output: Standard Output In the picture below you can see ...
- UVA 11174 Stand in a Line (组合+除法的求模)
题意:村子里有n个人,给出父亲和儿子的关系,有多少种方式可以把他们排成一列,使得没人会排在他父亲的前面 思路:设f[i]表示以i为根的子树有f[i]种排法,节点i的各个子树的根节点,即它的儿子为c1, ...
- UVA 10600 ACM Contest and Blackout 次小生成树
又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...
随机推荐
- 【iOS】Xcode 插件安装后无效
今天把 Xcode 升级到 7.3.1,发现插件安装后无效…… 原因:苹果要求加入UUID证书从而保证插件的稳定性. 解决方法: 1. 查看Xcode的UUID 在终端输入以下命令: defaults ...
- 从windows平台转战ubuntu
说到ubuntu,可能很多人会有些陌生,但对于有些人很熟悉.ubuntu是linux里面最为流行的一版,以下来自百度百科. Ubuntu(乌班图)是基于Debian GNU/Linux,支 ...
- 堆排序(实现c++)
堆可以看作是一个完全二叉树,分为大顶堆和小顶堆,本文我们以大顶堆为例来实现堆排序. (1)建堆 先把给定的序列转换成一棵完全二叉树,然后逐步对其调整使其每个结点的值都大于其两个子结点的值,因此我们需要 ...
- spark shuffle读操作
提出问题 1. shuffle过程的数据是如何传输过来的,是按文件来传输,还是只传输该reduce对应在文件中的那部分数据? 2. shuffle读过程是否有溢出操作?是如何处理的? 3. shuff ...
- Git使用(码云)
1.安装git软件(码云/GitHub) 2.码云注册,保存代码 3.创建代码托管仓库,仓库名相当于码云上的文件夹 4.写作业并提交 在作业文件夹上,右键选择‘get bash here’ 在黑框里输 ...
- 自己动手实现MQTT协议
写在前面 前段时间弄IoT相关的东西,系统学习了一下 MQTT 协议,在此分享出来. 本文先是对 MQTT 协议做了简单的介绍:接着是对 MQTT协议的内容做了较为全面的解读:最后使用 Python ...
- 存在于文件名中的SQL手工注入
SQL注入已经在前一章为大家介绍了个大概,本文将讲述我遇到的本以为是文件上传漏洞,却是以文件名触发的SQL注入! 本文分享的内容同样来自于一道CTF题! 1. 直接进入正题 (1) 初步探测 先看一下 ...
- 信安周报-第02周:SQL基础
信安之路 第02周 Code:https://github.com/lotapp/BaseCode/tree/master/safe 前言 本周需要自行研究学习的任务贴一下: 1.概念(推荐) 数据库 ...
- 前端利器躬行记(1)——npm
npm(Node Package Manager)是Node.js的包管理工具,相当于一个在线仓库.它提供了一个公共的平台,将分散在世界各地的包集中起来,能轻松的安装.分享和管理相关的包,不用再为搜索 ...
- 深入理解Mysql索引底层数据结构与算法
索引是帮助MySQL高效获取数据的排好序的数据结构 索引数据结构对比 二叉树 左边子节点的数据小于父节点数据,右边子节点的数据大于父节点数据. 如果col2是索引,查找索引为89的行元素,那么只需要查 ...