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 ...
随机推荐
- 最火的分布式 HTAP 数据库 TiDB - 入门实践教程
偶然在某篇博客看到了 TiDB,一个融合 OLTP 和 OLAP 的分布式开源数据库, GitHub 上 Star 很多,然后 watch 了,发现 commit 和 pull request 一直都 ...
- superset安装文档
1 安装python3.6 yum install epel-release -y yum install https://centos7.iuscommunity.org/ius-release.r ...
- 小白学python-day08-文件及其操作、字符串字典类型转换
今天是day08,以下是学习总结: 但行努力,莫问前程. ----------------------------------------------------------------------- ...
- linux字符设备驱动中内核如何调用驱动入口函数 一点记录
/* 内核如何调用驱动入口函数 ? *//* 答: 使用module_init()函数,module_init()函数定义一个结构体,这个结构体里面有一个函数指针,指向first_drv_init() ...
- Chrome谷歌浏览器实用插件
总结整理了一下个人平时常用的谷歌浏览器插件 Adblock Plus 广告拦截 uBlock Origin Chrono 下载管理器 Tampermonkey 油猴子(各种强大的脚本,强烈推荐) F ...
- 基于JaCoCo的Android测试覆盖率统计(二)
> 本文章是我上一篇文章的升级版本,详见地址:https://www.cnblogs.com/xiaoluosun/p/7234606.html ## 为什么要做这个?1. 辛辛苦苦写了几百条测 ...
- JDK1.8源码分析01之学习建议(可以延伸其他源码学习)
序言:目前有个计划就是准备看一下源码,来提升自己的技术实力.同时现在好多面试官都喜欢问源码,问你是否读过JDK源码等等? 针对如何阅读源码,也请教了我的老师.下面就先来看看老师的回答,也许会有帮助呢. ...
- Docker笔记(七):常用服务安装——Nginx、MySql、Redis
开发中经常需要安装一些常用的服务软件,如Nginx.MySql.Redis等,如果按照普通的安装方法,一般都相对比较繁琐 —— 要经过下载软件或源码包,编译安装,配置,启动等步骤,使用 Docker ...
- SpringMVC学习笔记之---数据绑定
SpringMVC数据绑定 一.基础配置 (1)pom.xml <dependencies> <dependency> <groupId>junit</gro ...
- Python 四大主流 Web 编程框架
Python 四大主流 Web 编程框架 目前Python的网络编程框架已经多达几十个,逐个学习它们显然不现实.但这些框架在系统架构和运行环境中有很多共通之处,本文带领读者学习基于Python网络框架 ...