URAL 1416 Confidential(次小生成树)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416
Input
Output
题目大意:给一个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(次小生成树)的更多相关文章
- URAL 1416 Confidentia [次小生成树]
题意: 第一行n m代表n个点m条无向边. 接下来m行每行abc,代表ab之间有一条长度为c的无向边. 求: 最小生成树的边权和 次小生成树的边权和 #include<stdio.h> ...
- URAL 1416 Confidential --最小生成树与次小生成树
题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...
- URAL 1416 Confidential (最小生成树+次小生成树)
Description Zaphod Beeblebrox - President of the Imperial Galactic Government. And by chance he is a ...
- kuangbin带你飞 生成树专题 : 次小生成树; 最小树形图;生成树计数
第一个部分 前4题 次小生成树 算法:首先如果生成了最小生成树,那么这些树上的所有的边都进行标记.标记为树边. 接下来进行枚举,枚举任意一条不在MST上的边,如果加入这条边,那么肯定会在这棵树上形成一 ...
- HDU 4081Qin Shi Huang's National Road System(次小生成树)
题目大意: 有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点.秦始皇希望这所有n-1条路长度之和最短.然后徐福突然有冒出来,说是他有魔法,可以不用人力.财力就变 ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- The Unique MST(次小生成树)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22335 Accepted: 7922 Description Give ...
- POJ1679The Unique MST(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25203 Accepted: 8995 D ...
- [kuangbin带你飞]专题八 生成树 - 次小生成树部分
百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...
随机推荐
- [Virtualization][SDN] 讲的很好的SDN软件定义网络视频课程
51CTO的免费课程,开始以为是扯蛋的,后来看了一下,讲的很好.注册一下,免费的. 只看了导论,挺好的. http://edu.51cto.com/course/course_id-4466.html
- eclipse dbviewer,eclipse java8
进入/home/xxx(用户名)/.local/share/applications,看是否有eclipse和深度音乐desktop配置文件,为eclipse.desktop配置图标, 那现在终端输入 ...
- 【Android测试】【第十节】MonkeyRunner—— 录制回放
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4861693.html 前言 在实际项目进行过程中,频繁的需 ...
- 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 ...
- ASP.NET MVC3更新出错:ObjectStateManager中已存在具有同一键的对象
程序代码: [HttpPost] public ActionResult Edit(Person person) { if (ModelState.IsValid) { Person oldperso ...
- 如何更改Magento的Base URL
Magento的Base URL是用于访问商店页面的URL,您也可以为单独一个store view设置一个Base Url.在改这项值之前请确保您的域名已经指向了网站所在服务器的IP,DNS解析完成后 ...
- js获取网站根目录
//js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址 function getRootPath(){ var strFullPath=window ...
- 一个例子深入理解ClassLoader
文件类加载器,该加载器重载了loadClass方法,逻辑是只读取文件来加载类,不委托给父类加载器进行加载 package com.ydd.study.hello.classloader; import ...
- POJ 1035题目描述
Description You, as a member of a development team for a new spell checking program, are to write a ...
- android中用Spannable在TextView中设置超链接、颜色、字体
昨晚研读 ApiDemo 源码至 com.example.android.apis.text.Link 类.首先,看一下其运行效果: 要给 TextView 加上效果,方式主要有几种: 第一种,自动 ...