思路:

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)题解的更多相关文章

  1. UVA 10462 Is There A Second Way Left? 次小生成树

    模板题 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdli ...

  2. UVA 10462 Is There A Second Way Left? (次小生成树+kruskal)

    题目大意: Nasa应邻居们的要求,决定用一个网络把大家链接在一起.给出v个点,e条可行路线,每条路线分别是x连接到y需要花费w. 1:如果不存在最小生成树,输出“No way”. 2:如果不存在次小 ...

  3. UVA - 10462-Is There A Second Way Left? Kruskal求次小生成树

    UVA - 10462 题意: 求次小生成树的模板题,这道题因为有重边的存在,所以用kruskal求比较好. #include <iostream> #include <cstdio ...

  4. 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 ...

  5. UVA - 10462 Is There A Second Way Left?

    题意: 给你一张无向图,让你判断三种情况:1.不是连通图(无法形成生成树)2.只能生成唯一的生成树 3.能生成的生成树不唯一(有次小生成树),这种情况要求出次小生成树的边权值和. 思路: 比较常见的次 ...

  6. UVA 10600 ACM Contest and Blackout 次小生成树

    又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...

  7. 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)

    [题意] n个点,m条边,求最小生成树的值和次小生成树的值. InputThe Input starts with the number of test cases, T (1 < T < ...

  8. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树

    题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...

  9. Qin Shi Huang's National Road System UVA - 1494(次小生成树)

    秦始皇统一中国之后要在全国修公路连接各个城市,皇帝只想修成最小生成树(距离最小,不考虑人力),一个道士说自己可以不花人力物力修一条路,经过两方妥协,选择max(两个城市人口/(生成树长度-这条路的长度 ...

随机推荐

  1. Balanced Lineup---poj3264线段树基础

    题目链接 求对应区间最大值与最小值的差: #include<stdio.h> #include<string.h> #include<algorithm> #inc ...

  2. 洛谷P2444 病毒 [POI2000] AC自动机

    正解:AC自动机 解题报告: 传送门! 首先看到这种题目二话不说先把trie树和fail指针建立起来 然后就想鸭,如果我们想让模式串和文本串尽量不能匹配,就要想办法让它跳fail指针,而不是继续往下走 ...

  3. zookeeper 版本不一致导致不断重连

    在使用kafka 和zookeeper 实现实时分析程序时,由于zookeeper部署版本和分析程序导入jar包的版本不一致,导致了当实时分析程序从远程服务器连接kafka集群的zookeeper时报 ...

  4. 64位windows 7下配置TortoiseGit(转)

    原文:http://our2848884.blog.163.com/blog/static/146854834201152325233854/ 最近感觉自己电脑上的代码太乱了,东一块.西一块……于是决 ...

  5. oracle(九)索引扫描

    (1)索引唯一扫描(index unique scan) (2)索引范围扫描(index range scan) (3)索引全扫描(index full scan) (4)索引快速扫描(index f ...

  6. 【Python】Pycharm2018激活方式【亲测好用】

    2.激活码激活 优点:Window.Mac.Ubantu都稳定有效,关键是这种激活方式不会产生其他影响 缺点:需要修改hosts文件 修改hosts文件将0.0.0.0 account.jetbrai ...

  7. iOS入门怎样选择Swift和objective-c

    版权声明:本文为博主原创文章,未经博主同意不得转载.博主微信:lofocus https://blog.csdn.net/cuibo1123/article/details/28261795 学oc吧 ...

  8. CentOS工作内容(六)双网卡带宽绑定bind teaming

    CentOS工作内容(六)双网卡带宽绑定bind  teaming Teaming功能是什么功能http://zhidao.baidu.com/link?url=cpcwl9LH4FSHJBaTW-e ...

  9. C#基础笔记(第十一天)

    1.复习字符串(1)字符串的不可变性(2)字符串的方法:1)Split() 分割 把字符串中不想要的内容分割掉 返回一个字符串类型的数组 可以添加StringSplitOption.RemoveEmp ...

  10. OCR学习及tesseract的一些测试

    最近接触OCR,先收集一些资料,包括成熟软件.SDK.流行算法. 1. 一个对现有OCR软件及SDK的总结,比较全面,包括支持平台.编程语言.支持字体语言.输出格式.相关链接等 http://en.w ...