UVA 10462 Is There A Second Way Left?(次小生成树&Prim&Kruskal)题解
思路:
Prim:
这道题目中有重边
Prim可以先加一个sec数组来保存重边的次小边,这样不会影响到最小生成树,在算次小生成树时要同时判断次小边(不需判断是否在MST中)
Kruskal:
Kruskal对重边就很友好了,不用考虑
原理是这样的:我们先找最小生成树并用used标记好哪些边是MST的边,然后我们暴力遍历每一条MST边被删去的情况,如果还能生成MST就找出这些MST最小的,这棵MST就是次小生成树
Prim代码:
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int N = 200+5;
const int INF = 0x3f3f3f3f;
using namespace std;
int n,m,p[N],Case = 1;
int mp[N][N],sec[N][N],dis[N],x[N],y[N];
int pre[N];
bool vis[N];
int Max[N][N]; //最大权值边
bool is[N][N]; //是否在MST中
void init(){
int a,b,c;
scanf("%d%d",&n,&m);
memset(mp,INF,sizeof(mp));
memset(sec,INF,sizeof(sec));
for(int i = 1;i <= m;i++){
scanf("%d%d%d",&a,&b,&c);
if(c < mp[a][b]){
sec[a][b] = sec[b][a] = min(mp[a][b],sec[a][b]); //保存次小边
mp[a][b] = mp[b][a] = c;
}
else{
sec[a][b] = sec[b][a] = min(c,sec[a][b]);
}
}
memset(vis,false,sizeof(vis));
vis[1] = true;
for(int i = 2;i <= n;i++){
dis[i] = mp[i][1];
pre[i] = 1;
}
}
void Prim(){
int mincost = 0;
memset(Max,0,sizeof(Max));
memset(is,false,sizeof(is));
for(int i = 1;i <= n - 1;i++){
int MIN = INF;
int point = -1;
for(int j = 1;j <= n;j++){
if(!vis[j] && dis[j] < MIN){
MIN = dis[j];
point = j;
}
}
if(point == -1){
printf("Case #%d : No way\n",Case++);
return;
}
vis[point] = true;
mincost += MIN;
is[point][pre[point]] = is[pre[point]][point] = true;
for(int j = 1;j <= n;j++){
if(vis[j] && j != point){
Max[j][point] = Max[point][j] = max(Max[pre[point]][j],dis[point]);
}
if(!vis[j] && dis[j] > mp[j][point]){
pre[j] = point;
dis[j] = mp[j][point];
}
}
}
int seccost = INF,flag = 0;
for(int i = 1;i <= n;i++){
for(int j = 1;j < i;j++){
if(mp[i][j] != INF && !is[i][j]){
flag = 1;
seccost = min(seccost,mincost - Max[i][j] + mp[i][j]);
}
if(sec[i][j] != INF){
flag = 1;
seccost = min(seccost,mincost - Max[i][j] + sec[i][j]);
}
}
}
if(!flag) printf("Case #%d : No second way\n",Case++);
else printf("Case #%d : %d\n",Case++,seccost);
}
int main() {
int T;
scanf("%d",&T);
while(T--){
init();
Prim();
}
return 0;
}
Kruskal代码:
#include<cmath>
#include<stack>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
const int N = 200+5;
const int INF = 0x3f3f3f3f;
using namespace std;
struct edge{
int u,v,w;
}e[N];
bool cmp(edge a,edge b){
return a.w < b.w;
}
int f[N],used[N];
int Case = 1;
int Find(int x){
return f[x] == x? x : f[x] = Find(f[x]);
}
void Kruskal(){
int n,m,a,b,c;
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i++){
scanf("%d%d%d",&a,&b,&c);
e[i].u = a;
e[i].v = b;
e[i].w = c;
}
sort(e+1,e+m+1,cmp);
for(int i = 0;i <= n;i++) f[i] = i;
int cnt = 0,tot = 0;
for(int i = 1;i <= m;i++){
int fx = Find(e[i].u);
int fy = Find(e[i].v);
if(fx != fy){
f[fx] = fy;
cnt++;
used[tot++] = i;
}
if(cnt == n - 1) break;
}
if(cnt < n - 1){
printf("Case #%d : No way\n",Case++);
return;
}
//次小生成树
int seccost = INF;
for(int i = 0;i < tot;i++){
for(int j = 0;j <= n;j++) f[j] = j;
int num = 0,cost = 0;
for(int j = 1;j <= m;j++){
if(j == used[i]) continue; //删边
int fx = Find(e[j].u);
int fy = Find(e[j].v);
if(fx != fy){
f[fx] = fy;
num++;
cost += e[j].w;
}
if(num == n - 1) break;
}
if(num == n-1) seccost = min(seccost,cost);
}
if(seccost == INF) printf("Case #%d : No second way\n",Case++);
else printf("Case #%d : %d\n",Case++,seccost);
}
int main() {
int T;
scanf("%d",&T);
while(T--){
Kruskal();
}
return 0;
}
UVA 10462 Is There A Second Way Left?(次小生成树&Prim&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应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...
- UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树
UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...
- 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?
题意: 给你一张无向图,让你判断三种情况: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(两个城市人口/(生成树长度-这条路的长度 ...
随机推荐
- [js]js中原型的继承
js继承01 思路: 单例/工厂/构造函数--演进到原型 搞清原型结构 原型继承 模拟系统原型继承 实现自己的继承 观察原型继承特点 演进到原型链这一步 //单例模式: 防止变量名冲突: // 思路: ...
- POJ2488:A Knight's Journey(dfs)
http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...
- Fisher精确检验【转载】
转自:https://en.wikipedia.org/wiki/Fisher%27s_exact_test https://www.cnblogs.com/Dzhouqi/p/3440575.htm ...
- PAT The Best Rank[未作]
1012 The Best Rank (25)(25 分) To evaluate the performance of our first year CS majored students, we ...
- oracle怎么恢复被覆盖的存储过程
在oracle数据库中,如果覆盖了之前的存储过程,那得赶紧闪回,时长越长闪回的可能性越小. 原理很简单,存储过程的定义就是数据字典,修改数据字典跟修改普通表的数据没有区别,此时会把修改前的内容放到un ...
- 2018-2019-2 网络对抗技术 20165324 Exp4:恶意代码分析
2018-2019-2 网络对抗技术 20165324 网络对抗技术 Exp4:恶意代码分析 课下实验: 实践目标 是监控你自己系统的运行状态,看有没有可疑的程序在运行. 是分析一个恶意软件,就分析E ...
- 使用TreeView加载XML文件
PS: 由于小弟初学编程,本文只写实现方式,代码写的不是很好请见谅! 1.需要读取的xml文档内容 2. 最终实现效果 3 貌似看起实现起来很复杂 但是想想还是挺简单 思路: 读取XML文档 →获 ...
- 认识GMT和UTC时间-附带地理知识
GMT-格林尼治标准时 GMT 的全名是格林威治标准时间或格林威治平时 (Greenwich Mean Time),这个时间系统的概念在 1884 年确立,由英国伦敦的格林威治皇家天文台计算并维护,并 ...
- 134. Gas Station(数学定理依赖题)
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- lnmp1.4 安装php fileinfo扩展 方法
第一步:在lnmp1.4找到php安装的版本 使用命令 tar -jxvf php-7.1.7.tar.bz2 解压 第二步: 在解压的php-7.1.7文件夹里找到fileinfo文件夹,然 ...