思路:

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. 杭电oj题目分类

    基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029.1032.1037.1040.1048.1056.1058.1 ...

  2. 非常可乐---hdu 1495(BFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意: 有3个杯子a b c:a=b+c:然后刚开始时只有a是满的,其它为空的,然后a b c三个之间互相 ...

  3. 数据库级别DDL操作监控审计、数据库触发器/服务器触发器

    关键词:数据库触发器/服务器触发器  ,数据库级别DDL操作监控审计,禁止修改登录名密码 [1]数据库级别DDL操作监控审计 转自2012示例库,只能数据库级别,不能实例级别 use database ...

  4. 005-四种常见的 POST 提交数据方式

    1.http请求方法 HTTP Method RFC Request Has Body Response Has Body Safe Idempotent Cacheable GET RFC 7231 ...

  5. 十天精通CSS3(1)

    什么是CSS3? CSS3是CSS2的升级版本,3只是版本号,它在CSS2.1的基础上增加了很多强大的新功能. 目前主流浏览器chrome.safari.firefox.opera.甚至360都已经支 ...

  6. Chrome Input框老是有输入记录的终极解决方案

    尤其是日期框,输入记录都挡住日期弹框了. 浏览器地址栏输入: chrome://settings/autofill,按钮关掉就可以了.

  7. PostgreSQL远程连接配置

    postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的listen_addresses项值设定为“* ...

  8. JVM、Java编译器和Java解释器

    作用: JVM:JVM有自己完善的硬件架构,如处理器.堆栈(Stack).寄存器等,还具有相应的指令系统(字节码就是一种指令格式).JVM屏蔽了与具体操作系统平台相关的信息,使得Java程序只需要生成 ...

  9. transition过度效果 + transform旋转360度

    css样式: .animate{ width:65px; height:40px; background:#92B901; color:#ffffff; position:absolute; font ...

  10. Object-C-Foundation-NSDate

    NSDate 表达日期表达时间的方法 NSDate *now=[NSDate date]; 获得当前日期 NSDate *tomrrow=[now dateByAddingTimeInterval:2 ...