vijos1070 新年趣事之游戏 - 次小生成树
题目大意:
求原图的最小生成树,和次小生成树。
题目分析:
kruskals求mst(\(O(mlogm)\))
考虑次小生成树暴力的做法,因为次小生成树总是由最小生成树删掉一条边并添加一条边得到的,所以可以枚举最小生成树上的每一条边删去,再重新求一遍mst。(\(O(m^2logm)\))
下面的题解来自转载:(\(O(n^2(求最大权值) + mlogm(求最小生成树) + m(求次小))\))

code
#include<bits/stdc++.h>
using namespace std;
const int N = 550, M = 150050, OO = 0x3f3f3f3f;
int n, ans, m;
struct node{
int x, y, dis;
inline bool operator < (const node &b) const{
return dis < b.dis;
}
}edge[M];
int d[N][N];
bool vst[N], used[M];
namespace mst{
int ecnt, adj[N], nxt[M << 1], go[M << 1], len[M << 1];
inline void addEdge(int u, int v, int c){
nxt[++ecnt] = adj[u], adj[u] = ecnt, go[ecnt] = v, len[ecnt] = c;
nxt[++ecnt] = adj[v], adj[v] = ecnt, go[ecnt] = u, len[ecnt] = c;
}
int anc[N];
inline int getAnc(int x){
return x == anc[x] ? x : (anc[x] = getAnc(anc[x]));
}
inline int kruskals(){
int ret = 0;
sort(edge + 1, edge + m + 1);
for(int i = 1; i <= m; i++){
int fx = getAnc(edge[i].x), fy = getAnc(edge[i].y);
if(fx != fy) anc[fx] = fy, ret += edge[i].dis, addEdge(edge[i].x, edge[i].y, edge[i].dis), used[i] = true;
}
return ret;
}
inline void dfs(int now, int u, int f, int mx){
d[now][u] = d[u][now] = mx;
for(int e = adj[u]; e; e = nxt[e]){
int v = go[e];
if(v == f) continue;
dfs(now, v, u, max(mx, len[e]));
}
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) mst::anc[i] = i;
for(int i = 1; i <= m; i++){
int x, y, c; scanf("%d%d%d", &x, &y, &c);
edge[i] = (node){x, y, c};
}
ans = mst::kruskals();
if(n - 1 == mst::ecnt / 2) printf("Cost: %d\n", ans);
else printf("Cost: -1\nCost: -1\n");
int ans1 = OO;
for(int i = 1; i <= n; i++)
mst::dfs(i, i, 0, 0);
for(int i = 1; i <= m; i++){
if(used[i]) continue;
int x = edge[i].x, y = edge[i].y;
ans1 = min(ans1, ans - d[x][y] + edge[i].dis);
}
if(ans1 != OO) printf("Cost: %d", ans1);
else printf("Cost: -1");
return 0;
}
vijos1070 新年趣事之游戏 - 次小生成树的更多相关文章
- vijos次小生成树
xiaomengxian的哥哥是一个游戏迷,他喜欢研究各种游戏.这天,xiaomengxian到他家玩,他便拿出了自己最近正在研究的一个游戏给xiaomengxian看.这个游戏是这样的:一个国家有N ...
- 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 ...
- URAL 1416 Confidential --最小生成树与次小生成树
题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...
- POJ1679The Unique MST(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25203 Accepted: 8995 D ...
- [kuangbin带你飞]专题八 生成树 - 次小生成树部分
百度了好多自学到了次小生成树 理解后其实也很简单 求最小生成树的办法目前遇到了两种 1 prim 记录下两点之间连线中的最长段 F[i][k] 之后枚举两点 若两点之间存在没有在最小生成树中的边 那么 ...
- URAL 1416 Confidential(次小生成树)
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1416 Zaphod Beeblebrox — President of the Impe ...
- ACM题目————次小生成树
Description 最小生成树大家都已经很了解,次小生成树就是图中构成的树的权值和第二小的树,此值也可能等于最小生成树的权值和,你的任务就是设计一个算法计算图的最小生成树. Input 存在多组数 ...
随机推荐
- 动态规划例子:Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- Dynamics CRM 2016 Web API 消息列表
Function Name Description CalculateTotalTimeIncident Function Calculates the total time, in minutes, ...
- [Mobx] Use MobX actions to change and guard state
This lesson explains how actions can be used to control and modify the state of your application. Th ...
- [Angular] HttpParams
It is possible to use HttpParams to set http params. For example we have this url to make: https://a ...
- 分治法(divide & conquer)与动态规划(dynamic programming)应用举例
动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...
- Altium Designer如何统一改变pcb状态下的原件标号位置
原创 我用的是Altium Designer16版本 变成 步骤如下: 选中标号 右击 下边一步很重要: 点击应用和确定 在之后弹出的对话框中选则你要改变的位置,我这里是把标号改变到原件的右侧: 等待 ...
- 在react底下安装环境
1.在react底下安装环境 Image.png Image.png 2.新建一个文件夹 Image.png 3.配置入口文件redux:staticRoot+'/redux/app' Image.p ...
- 关于mysql事务行锁for update实现写锁的功能
关于mysql事务行锁for update实现写锁的功能 读后感:用切面编程的理论来讲,数据库的锁对于业务来说是透明的.spring的事务管理代码,业务逻辑代码,表锁,应该是三个不同的设计层面. 在电 ...
- Source Insight 3.50.0065使用详解
转自calvinlee1984 Subject:Source Insight3.50.0065使用详解 Date: 21-Oct-2011 By: Calvinlee1984@ ...
- autohotkey 自动登录输入用户名密码 getElementsByTagName/getElementsByClassName/getElementById
针对button未设置id的.可以通过getElementsByTagName获取button的对象数组,再明确其在对象数组中的位置,如第4个button,通过[3]获取.再调用此对象的click() ...
