题目链接: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. iOS开发之Objective-c的MD5/SHA1加密算法的实现

    Objective-c实现MD5和SHA1算法相对还是比较简单的,可以直接调用系统的C/C++共享库来实现调用 MD5即Message Digest Algorithm 5(信息-摘要算法 5),用于 ...

  2. Oracle一些基本操作

    查看表以及列: Select * From all_tables where owner = 'userName' ---注意,这里需要区分大小写! select * from user_tab_co ...

  3. win环境下,用虚拟化工具打包Qt动态编译exe的过程(使用Enigma Virtual Box)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://goldlion.blog.51cto.com/4127613/834075 引子 ...

  4. Qt high DPI

    http://doc.qt.io/qt-5/highdpi.html Qt Support Ability to provide pixmaps or artwork for high resolut ...

  5. Node的Buffer

    var buf3 = new Buffer([1,2,3,4,-10,256],'utf8');//默认为utf8 console.log(buf3[0]);//正常的范围是0~255 console ...

  6. Cocos2d-JS目录说明

    frameworks---- 引擎所在,包含两个文件夹cocos2d-html5 和js-bindings.前者是html5引擎,后者是-x的引擎,外部接口是js写,但基础模块却是cpp来实现. sa ...

  7. 搭建PHP环境LAMP(Linux+Apache+MySQL+PHP)

     1.安装Apache yum -y install httpd 用/etc/init.d/httpd start 启动apache apache默认的程序目录是/var/www/html 2.安装M ...

  8. 20145211 《Java程序设计》第2周学习总结——桃花依旧笑春风

    教材学习内容总结 基本类型 整数 short 2字节,int 4字节,long 8字节 字节 byte 1字节 浮点数 float 4字节,double 8字节 字符 char 2字节(包括字母.汉字 ...

  9. 2014-4-25 运行号:837344 ASCII码排序

    #include <iostream> #include <cstdio> #include <cstdlib> #include <string> # ...

  10. sql server 2008查询窗口怎么显示行数

    工具->选项