题目链接: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. [Virtualization][SDN] 讲的很好的SDN软件定义网络视频课程

    51CTO的免费课程,开始以为是扯蛋的,后来看了一下,讲的很好.注册一下,免费的. 只看了导论,挺好的. http://edu.51cto.com/course/course_id-4466.html

  2. eclipse dbviewer,eclipse java8

    进入/home/xxx(用户名)/.local/share/applications,看是否有eclipse和深度音乐desktop配置文件,为eclipse.desktop配置图标, 那现在终端输入 ...

  3. 【Android测试】【第十节】MonkeyRunner—— 录制回放

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4861693.html 前言 在实际项目进行过程中,频繁的需 ...

  4. insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a=10001

    insert into hi_user_score set hello_id=74372073,a=10001 on duplicate key update hello_id=74372073, a ...

  5. ASP.NET MVC3更新出错:ObjectStateManager中已存在具有同一键的对象

    程序代码: [HttpPost] public ActionResult Edit(Person person) { if (ModelState.IsValid) { Person oldperso ...

  6. 如何更改Magento的Base URL

    Magento的Base URL是用于访问商店页面的URL,您也可以为单独一个store view设置一个Base Url.在改这项值之前请确保您的域名已经指向了网站所在服务器的IP,DNS解析完成后 ...

  7. js获取网站根目录

    //js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址         function getRootPath(){        var strFullPath=window ...

  8. 一个例子深入理解ClassLoader

    文件类加载器,该加载器重载了loadClass方法,逻辑是只读取文件来加载类,不委托给父类加载器进行加载 package com.ydd.study.hello.classloader; import ...

  9. POJ 1035题目描述

    Description You, as a member of a development team for a new spell checking program, are to write a ...

  10. android中用Spannable在TextView中设置超链接、颜色、字体

    昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类.首先,看一下其运行效果:  要给 TextView 加上效果,方式主要有几种: 第一种,自动 ...