POJ1679(次小生成树)
The Unique MST
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 36692 | Accepted: 13368 |
Description
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.
Input
Output
Sample Input
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
Sample Output
3
Not Unique! 题解:
次小生成树,维护一个两点间的最小距离,最后再向上加
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
#define line cout<<"------------------"<<endl;
const int MAXN=1e4+10;
const int INF=0x3f3f3f3f;
int n,m;
struct node{
int x,y;
int v;
bool vis;
}Edge[MAXN];
bool cmp(node a,node b)
{
return a.v<b.v;
}
int pre[MAXN];
int Find(int a)
{
if(pre[a]==a)
return a;
return Find(pre[a]);
}
vector<int >G[110]; int maxd[110][110];//并查集划到一个树上后,树上任意两点之间的距离 void init()
{
for (int i = 1; i <=n; ++i) {
G[i].clear();
pre[i] = i;
G[i].push_back(i);
} }
int main()
{
int _;
scanf("%d",&_);
while(_--)
{
scanf("%d%d",&n,&m);
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&Edge[i].x,&Edge[i].y,&Edge[i].v);
Edge[i].vis=false;
}
sort(Edge+1,Edge+1+m,cmp);
int sum=0;
for (int i = 1; i <=m ; ++i) {
int x=Find(Edge[i].x);
int y=Find(Edge[i].y);
if(x!=y)
{
pre[x]=y;
sum+=Edge[i].v;
int len1=G[x].size();
int len2=G[y].size();
for (int j = 0; j <len1 ; ++j) {
for (int k = 0; k <len2 ; ++k) {
maxd[G[x][j]][G[y][k]]=maxd[G[y][k]][G[x][j]]=Edge[i].v;//构建两点间最小距离
}
}
int tem[110];
for (int j = 0; j <len2 ; ++j) {
tem[j]=G[y][j];
}
for (int j = 0; j <len1 ; ++j) {
G[y].push_back(G[x][j]);
}
for (int j = 0; j <len2 ; ++j) {
G[x].push_back(tem[j]);
}
Edge[i].vis=true;
}
}
int cis=INF;
for (int i = 1; i <=m ; ++i) {//从不是最小生成树上的边,遍历向上加。找到次小生成树
if(!Edge[i].vis)
cis=min(cis,sum+Edge[i].v-maxd[Edge[i].x][Edge[i].y]);
}
if(cis>sum)
printf("%d\n",sum);
else
printf("Not Unique!\n");
} return 0;
}
//poj1679
POJ1679(次小生成树)的更多相关文章
- POJ1679(次小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24201 Accepted: 8596 D ...
- poj1679次小生成树入门题
次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即求最小生成树时求得 ...
- poj1679 次小生成树
prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- 次小生成树(POJ1679/CDOJ1959)
POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生 ...
- POJ1679 The Unique MST【次小生成树】
题意: 判断最小生成树是否唯一. 思路: 首先求出最小生成树,记录现在这个最小生成树上所有的边,然后通过取消其中一条边,找到这两点上其他的边形成一棵新的生成树,求其权值,通过枚举所有可能,通过这些权值 ...
- POJ1679 The Unique MST 【次小生成树】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20421 Accepted: 7183 D ...
- 次小生成树(poj1679)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20737 Accepted: 7281 D ...
- POJ1679 The Unique MST —— 次小生成树
题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total S ...
随机推荐
- VSCode cpptools 插件在Centos 7下不能正确显示符号列表的解决办法
vscode 的插件cpptools 0.9.3 需要glibc 2.18的版本,但是Centos 7 下没有这个版本的GLIBC,所以导致链接库丢失,后台服务不能正常运行.按以下步骤操作可修复此问题 ...
- March 8 2017 Week 10 Wednesday
Rules are meant to be broken. 规则就是用来被打破的. What is innovation? Some may tell you innovation is to bre ...
- jsp页面传输到xxAction.java乱码解决
jsp页面传输到xxAction.java乱码解决:jsp:encodeURI(encodeURI("xx"))java:if(!StringUtils.isBlank(belon ...
- OC 构造方法
#import <Foundation/Foundation.h> @interface Student : NSObject { int _age; int _no; } - (void ...
- wireshark抓取本地回环数据包
linux环境下,用tcpdump,可以用-i lo参数抓取环回接口的包.如果服务端和客户端安装在同一台机器上,调试时是很方便的.linux版的wireshark,选取网卡的菜单里也有lo选项,也 ...
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- [19/03/24-星期日] 容器_Collection(集合、容器)之List(表,有顺序可重复)
一. 概念&方法 Collection 表示一组对象,它是集中.收集的意思.Collection接口的两个子接口是List.Set接口. 由于List.Set是Collection的子接口,意 ...
- 【洛谷P1169】[ZJOI2007]棋盘制作
棋盘制作 题目链接 这个题是[USACO5.3]巨大的牛棚Big Barn和玉蟾宫的结合 一道顶两道毒瘤! 题解: 首先,棋盘有两种选法: 1.任意白格(x,y) (x+y)%2=0 ,任意黑格(x, ...
- C# 基础(一) 访问修饰符、ref与out、标志枚举等等
C# 基础(一) 访问修饰符.ref与out.标志枚举等等 一.访问修饰符 在C#中的访问修饰符有:private.protected.internal.public public:公共类型,同一程序 ...
- Mac iOS 允许从任何来源下载应用并打开
一个快捷的小知识点,mark! 允许从任何来源下载应用并打开,不用手动去允许,更加简洁! 只需一行命令 sudo spctl --master-disable 1.正常情况下,打开偏好设置,选择安全性 ...