题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416

Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.

Input

The first input line contains two integers: N (2 ≤ N ≤ 500) is a number of planets in the Galaxy and M is an amount of possible transitions. The next M lines contain three integers aibi the numbers of the planets that are connected with some passage (1 ≤ aibi ≤ N), and wi (0 ≤ wi ≤ 1000) is the transition cost. If an A to B transition is possible then a B to A transition is possible, too. The cost of those transitions are equal. There is not more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.

Output

You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by −1.

题目大意:给一个n个点m条边的无向图,求最小生成树和次小生成树(图无重边,似乎是连通图)。

思路:参考2014年汪汀的IOI集训队论文《最小生成树问题的拓展》。时间复杂度为O(n^2)。

代码(0.109MS):

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std; const int MAXV = ;
const int MAXE = MAXV * MAXV;
const int INF = 0x3f3f3f3f; int head[MAXV], ecnt;
int to[MAXE], next[MAXE], cost[MAXE];
bool select[MAXE];
int n, m; void init() {
memset(head + , -, n * sizeof(int));
ecnt = ;
} void add_edge(int u, int v, int c) {
to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;
} int dis[MAXV], pre[MAXV];
int maxPath[MAXV][MAXV];
bool vis[MAXV]; int prim() {
memset(dis + , 0x3f, n * sizeof(int));
dis[] = ;
int res = ;
for(int _ = ; _ < n; ++_) {
int u = -;
for(int i = ; i <= n; ++i) if(!vis[i] && dis[i] < INF)
if(u == - || dis[i] < dis[u]) u = i;
if(u == -) return -;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(vis[v]) continue;
if(cost[p] < dis[v]) dis[v] = cost[p], pre[v] = p;
}
for(int i = ; i <= n; ++i) if(vis[i]) {
int &v = to[pre[u] ^ ];
maxPath[i][u] = maxPath[u][i] = max(maxPath[i][v], dis[u]);
}
res += dis[u];
vis[u] = true;
if(u != ) select[pre[u]] = select[pre[u] ^ ] = true;
}
return res;
} int solve(int ans) {
int res = -;
for(int u = ; u <= n; ++u) {
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(select[p] || u == v) continue;
if(res == - || ans - maxPath[u][v] + cost[p] < res)
res = ans - maxPath[u][v] + cost[p];
}
}
return res;
} int main() {
scanf("%d%d", &n, &m);
init();
for(int i = , a, b, c; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
add_edge(a, b, c);
}
int ans1 = prim(), ans2 = -;
if(ans1 != -) ans2 = solve(ans1);
printf("Cost: %d\n", ans1);
printf("Cost: %d\n", ans2);
}

URAL 1416 Confidential(次小生成树)的更多相关文章

  1. URAL 1416 Confidentia [次小生成树]

    题意: 第一行n m代表n个点m条无向边. 接下来m行每行abc,代表ab之间有一条长度为c的无向边. 求: 最小生成树的边权和  次小生成树的边权和 #include<stdio.h> ...

  2. URAL 1416 Confidential --最小生成树与次小生成树

    题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...

  3. URAL 1416 Confidential (最小生成树+次小生成树)

    Description Zaphod Beeblebrox - President of the Imperial Galactic Government. And by chance he is a ...

  4. kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数

    第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...

  5. HDU 4081Qin Shi Huang's National Road System(次小生成树)

    题目大意: 有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点.秦始皇希望这所有n-1条路长度之和最短.然后徐福突然有冒出来,说是他有魔法,可以不用人力.财力就变 ...

  6. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  7. The Unique MST(次小生成树)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22335   Accepted: 7922 Description Give ...

  8. POJ1679The Unique MST(次小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25203   Accepted: 8995 D ...

  9. [kuangbin带你飞]专题八 生成树 - 次小生成树部分

    百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...

随机推荐

  1. Hadoop学习笔记(一)

    HDFS适合一次写入,多次读取NameNode将文件系统的元数据存储在内存中,因此HDFS所能存储的文件总数受限于NameNode容量类:IOUtil Progressable URL.setURLS ...

  2. jquery rotate

    网上发现一个很有意思的jQuery旋转插件,支持Internet Explorer 6.0+ .Firefox 2.0 .Safari 3 .Opera 9 .Google Chrome,高级浏览器下 ...

  3. Gulp自动化工具之图片压缩

    一.安装node https://nodejs.org/download/ 根据需要选择对应的版本 安装好了之后可以通过node -v参看一下版本 node -v 二.安装gulp npm insta ...

  4. ArcGIS API for Silverlight加载google地图(后续篇)

    原文:ArcGIS API for Silverlight加载google地图(后续篇) 之前在博客中(http://blog.csdn.net/taomanman/article/details/8 ...

  5. 10 Golden Rules of Project Risk Management

    The benefits of risk management in projects are huge. You can gain a lot of money if you deal with u ...

  6. SQL Server select 将类型相同的行合并,并将对应金额相加

    select Category,REPLACE(sum(Amount),'-','')  as Amountfrom T_Detail WHERE CREATED_BY='6123EC14-50E2- ...

  7. imx6 android 进入文件系统闪屏

    imx6进入文件系统的时候都会闪屏,应该是framebuffer未初始化,就已经打开了背光.目前解决办法,在kenel阶段关闭背光,显示android的开机动画之后(此时framebuffer已经初始 ...

  8. opencv的高斯混合模型

    http://blog.jasonding.top/2015/04/05/Machine%20Learning/%E3%80%90%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%8 ...

  9. saltstack之(一)系统环境及本地yum源

    1.服务器环境node1:192.168.3.1node2:192.168.3.2 2.主机名和hosts文件node1: node1.xkops.com --主机名[root@node1 ~]# t ...

  10. linux chmod命令(转)

    chmod命令用于改变linux系统文件或目录的访问权限.用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法. Linux系统中的每 ...