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 ...
随机推荐
- Zeppelin interperter 模式设置总结图解1
如有错漏,望请指正,不胜感激. 参考:[zeppelin官网]:https://zeppelin.apache.org/docs/latest/interpreter/spark.html#inter ...
- JAVA / MySql 编程——第七章 JDBC
1.JDBC:JDBA是Java数据库连接(Java DataBase Connectivity)技术的简称,提供连接各种常用数据库的能力: ●Java是通过JDBC技术实现对各种数据 ...
- git 代码托管使用方法
Git代码托管 1 准备材料 在coding,github这些代码托管网站上申请一个账户. Linux平台什么需要一个git,如ubuntu 需要 $ sudo apt-get install git ...
- Python全栈day 04
Python全栈day 04 一.解释器/编译器 补充:编译型语言和解释型语言? # 编译型:代码写完后,编译器将其变成成另外一个文件,然后交给计算机执行. c c++,c# ,java # 解释型: ...
- keil5的安装及问题
win8+keil 注意,在进行破解的时候首先要打开一个工程,而且keil要用管理员的身份进行运行, 才可以破解完成 发现打开之后,出现这样的错误. 原因是因为在创建工程的时候在下图中点了是,要点否才 ...
- python创建字典
创建: {x:x**2 for x in (2,4,6)} dict(xjm=110,lxh=119,pzq=120) dict([('a',1),('b',2),('c',3)])
- Docker使用入门
docker images 查看本地镜像 docker ps -a 查询容器 docker ps -l 查询最近使用容器 docker rm CONTAINER_ID 删除容器 docker rm ...
- oracle11g导出表时空表导不出解决方案
oracle11g用exp命令导出数据库表时,有时会发现只导出了一部分表时而且不会报错,原因是有空表没有进行导出,之前一直没有找到方法于是用最笨的方法重新建这些空表,当然在我们实际当中表的数量大时我们 ...
- linux 检测进程是否存在
1. 直接遍历/proc目录 int find_pid_by_name( char* pidname, pid_t *pidlist) { #define READ_BUF_SIZE 256 DIR ...
- shell判断文件是否存在[转]
原文出处: http://canofy.iteye.com/blog/252289 shell判断文件,目录是否存在或者具有权限 #!/bin/sh myPath="/var/log/htt ...