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 ... 
随机推荐
- .net core(c#)拟合圆测试
			说明 很多时候,我们需要运动物体的转弯半径去描述其机器性能.但在大多数的现实条件下,我们只能够获取到运动物体的 GPS 位置点集,并不能直接得到转弯半径或者圆心位置.为此,我们可以利用拟合圆的方式得到 ... 
- 夯实Java基础(八)——代码块
			在Java中代码块指的是使用”{}”括起来的代码称为代码块.代码块一共分为4种:局部代码块,静态代码块,同步代码块,构造代码块. 1.局部代码块 局部代码块就是定义在方法体内部的代码块. public ... 
- js中判断一个对象的类型的种种方法
			javascript中检测对象的类型的运算符有:typeof.constructor.instanceof. typeof:typeof是一个一元运算符,返回结果是一个说明运算数类型的字符串.如:&q ... 
- Spark 系列(三)—— 弹性式数据集RDDs
			一.RDD简介 RDD 全称为 Resilient Distributed Datasets,是 Spark 最基本的数据抽象,它是只读的.分区记录的集合,支持并行操作,可以由外部数据集或其他 RDD ... 
- django实现自定义manage命令的扩展
			在Django开发过程中我们都用过django-admin.py和manage.py命令. django-admin.py是一个命令行工具,可以执行一些管理任务,比如创建Django项目.而manag ... 
- 史上最全面的SignalR系列教程-3、SignalR 实现推送功能-集线器类实现方式
			1.概述 通过前两篇 史上最全面的SignalR系列教程-1.认识SignalR 史上最全面的SignalR系列教程-2.SignalR 实现推送功能-永久连接类实现方式 文章对SignalR的介绍, ... 
- Appium+python自动化(三十二)- 代码写死一时爽,框架重构火葬场 - PageObject+unittest(超详解)
			简介 江湖有言:”代码写死一时爽,框架重构火葬场“,更有人戏言:”代码动态一时爽,一直动态一直爽 
- 云上RDS架构
			概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ... 
- spark通过JDBC读取外部数据库,过滤数据
			官网链接: http://spark.apache.org/docs/latest/sql-programming-guide.html#jdbc-to-other-databases http:// ... 
- win server 2008搭建域环境
			0x00 简介 1.域控:win server 2008 2.域内服务器:win server 2008.win server 2003 3.域内PC:win7 x64.win7 x32.win xp ... 
