题目链接: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. Python 时间 日期常见操作

    import datetime,time dtstr = '2014-02-14 21:32:12' a = datetime.datetime.strptime(dtstr, "%Y-%m ...

  2. 汇编查看StackFrame栈帧

    INCLUDE Irvine32.inc myProc PROTO, x:DWORD, y:DWORD .data .code main proc mov eax,0EAEAEAEAh mov ebx ...

  3. eclipse根据.wsdl文件自动生成webservice的调用客户端

    1.工具:eclipse3.3或者是带有webservice插件的eclipse 2. 首先用浏览器访问webservice的站点,接着保存打开的页面,后缀为.wsdl. 3.把保存好的文件拷入ecl ...

  4. 阅读javaScript 的原型笔记

    下面我们先看一个例子已经一张图. function Foo() { } Object.prototype.name = 'My Object'; Foo.prototype.name = 'Bar'; ...

  5. linux 开机启动设置

    操作系统:Ubuntu12.04硬件环境:HP CQ45        当用户使用sudo apt-get install安装完apache和mysql之后,这些服务默认是开机启动的,但是有的时候需要 ...

  6. [LeetCode]题解(python):094 Binary Tree Inorder Traversal

    题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the ino ...

  7. nginx支持cgi(c,c++)

    前段时间用我修改了tinyhttpd,让其cgi支持文件流,感觉满小巧,就应用上了.最近访问请求量上来而来,它对socket的各种异常状态处理不好,对于慢速的链接会占用我的线程.虽然我一直想仿出ten ...

  8. miaov- 自动生成正V反V大于号V小于号V楼梯等图案

    1. 核心:控制 数量的长度-1-i的位置,是放在left上还是top上?是放在前面还是后面! <!DOCTYPE html> <html lang="en"&g ...

  9. java.lang.NoClassDefFoundError: Could not initialize class ......

    在测试数据字典工具类的时候一直报这个错误,找了好久,原来是SpringContextUtils这个类没有放入到容器中. 但是我在SpringContextUtils上面加了注解的,为什么注解没有扫到呢 ...

  10. SQL判断字符串里不包含字母

    Oracle: 方法一:通过To_Number 函数异常来判断,因为这个函数在转换不成功的时候是报错,所以只能用存储过程包装起来. CREATE OR REPLACE FUNCTION Is_Numb ...