POJ -1679(次小生成树)模板
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions:34617 | Accepted: 12637 |
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<set>
#include<map>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<string>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull; const int maxn = + ;
const int maxv = + ;
const int max_dis = 1e9 + ; int T;
int n,m;
int a,b,c;
int MST,_MST;
bool vis[maxn];
int father[maxn];
int dist[maxn];
int graph[maxn][maxn];
bool used[maxn][maxn];
int max_edge[maxn][maxn]; void init() {
memset(vis,false,sizeof(vis));
memset(used,false,sizeof(used));
memset(max_edge,-,sizeof(max_edge));
memset(graph,0x7f,sizeof(graph));
} void input() {
scanf("%d%d",&n,&m);
for(int i=; i<m; i++) {
scanf("%d%d%d",&a,&b,&c);
graph[a][b]=graph[b][a]=c;
used[a][b]=used[b][a]=true;
}
} int prim() {
int ans=;
dist[]=;
vis[]=true;
father[]=-;
for(int i=; i<=n; i++) {
father[i]=;
dist[i]=graph[][i];
}
for(int i=; i<n; i++) {
int v=-;
for(int j=; j<=n; j++) {
if(!vis[j]&&(v==-||dist[j]<dist[v])) v=j;
}
ans+=dist[v];
vis[v]=true;
used[father[v]][v]=used[v][father[v]]=false;
for(int j=; j<=n; j++) {
if(vis[j]) {
max_edge[v][j]=max_edge[j][v]=max(max_edge[father[v]][j],dist[v]);
} else {
if(graph[v][j]<dist[j]) {
dist[j]=graph[v][j];
father[j]=v;
}
}
}
}
return ans;
} int second_prim() {
int ans=max_dis;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(used[i][j]) ans=min(ans,MST+graph[i][j]-max_edge[i][j]);
return ans;
}
void solve() {
MST=prim();
_MST=second_prim();
if(MST==_MST) printf("Not Unique!\n");
else printf("%d\n",MST);
}
int main() {
scanf("%d",&T);
while(T--) {
init();
input();
solve();
}
return ;
}
POJ -1679(次小生成树)模板的更多相关文章
- poj 2831 次小生成树模板
/*次小生成树 题意:给你一些路径,现在将一部分路径权值减少后问是否可以替代最小生成树里面的边. 解:次小生成树,即将这条边连上,构成一个环 求出任意两点路径之间的除了这条边的最大值,比较这个最大值& ...
- poj 1679 次小生成树
次小生成树的求法: 1.Prime法 定义一个二维数组F[i][j]表示点i到点j在最小生成树中的路径上的最大权值.有个知识就是将一条不在最小生成树中的边Edge加入最小生成树时,树中要去掉的边就是E ...
- The Unique MST POJ - 1679 次小生成树prim
求次小生成树思路: 先把最小生成树求出来 用一个Max[i][j] 数组把 i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- The Unique MST POJ - 1679 (次小生成树)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- poj1679The Unique MST(次小生成树模板)
次小生成树模板,别忘了判定不存在最小生成树的情况 #include <iostream> #include <cstdio> #include <cstring> ...
- poj 1679 The Unique MST (次小生成树模板题)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- POJ_1679_The Unique MST(次小生成树模板)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23942 Accepted: 8492 D ...
- POJ-1679 The Unique MST,次小生成树模板题
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Description Given a connected undirec ...
- uva10600次小生成树模板题
裸题,上模板就行,注意j ! = k #include<map> #include<set> #include<cmath> #include<queu ...
随机推荐
- 【shopex】真正可用的app开发机制
shopex的app开发机制详解 网上流传的shopex4.8.5的app开发教程,不仅说得不明不白,而且由于版本问题,照着做根本是做不成的. 知其然,亦要知其所以然. shopex提供了的一个干 ...
- php中const与static的区别与使用(转)
首先关于const 在php的类内部只可以修饰成员属性,不可以修饰方法,如下: class Test{ const PATH = 'c/';//修饰常量 const function te ...
- Makefile (1) gcc基础
.c(源文件) --> .i(预处理文件) -->.s(汇编文件) --> -o(目标文件) -->可执行文件 .c --预处理-->.i .i --编译--> ...
- PAT (Basic Level) Practice 1040 有几个PAT
个人练习 字符串 APPAPT 中包含了两个单词 PAT,其中第一个 PAT 是第 2 位(P),第 4 位(A),第 6 位(T):第二个 PAT 是第 3 位(P),第 4 位(A),第 6 位( ...
- powershell设置SS代理
$env:HTTPS_PROXY="http://127.0.0.1:1080" $env:HTTP_PROXY="http://127.0.0.1:1080"
- centos使用--vim配置和推荐插件使用
目录 1.vimrc的配置内容 2.Vundle使用 简介 安装vundle 配置vundle插件: 安装需要的插件 移除不需要的插件 其他常用命令 3 使用插件 3.1 NERDTree 3.2 e ...
- MySQL高可用之MHA安装
Preface MasterHA is a tool which can be used in MySQL HA architecture.I'm gonna implement it ...
- 基于mysqldump备份集来恢复某个误操作的表(drop,truncate)
Preface How to rescue a dropped or truncated table online?Dropping or truncating is ddl oper ...
- SecureCRT自动登录跳板机/堡垒机并连接目标机器
公司登录目标服务器,需要先登录跳板机(堡垒机),然后再登录目标机器,一共需要4.5步. MAC或LINUX机器可以写.SH脚本,那WINDOWS有没有一键登陆的方法呢? 常用的SecureCRT工具就 ...
- Windows下如何解决git bash的默认home目录路径问题
转自:http://blog.csdn.net/lucien_zhou/article/details/62069246 为了解决这个问题,我在网上找了好久,尝试过按网上其他人所述,修改 git 安装 ...