(最小生成树 次小生成树)The Unique MST -- POJ -- 1679
链接:
http://poj.org/problem?id=1679
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/K
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 24594 | Accepted: 8751 |
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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3f3f3f3f; int J[N][N], dist[N], pre[N], Max[N][N], n, m;
int use[N][N];
bool vis[N]; int prim()
{
int i, j, ans=;
memset(Max, , sizeof(Max));
memset(use, , sizeof(use));
memset(dist, , sizeof(dist));
memset(vis, false, sizeof(vis));
vis[]=; for(i=; i<=n; i++)
{
dist[i]=J[][i];
pre[i]=;
} for(i=; i<n; i++)
{
int index=, MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
MIN=dist[j];
index=j;
}
}
ans += MIN;
vis[index]=;
use[index][pre[index]]=use[pre[index]][index]=; for(j=; j<=n; j++)
{
if(vis[j] && j!=index)
{
Max[index][j]=Max[j][index]=max(Max[j][pre[index]], dist[index]);
}
if(!vis[j] && dist[j]>J[index][j])
{
dist[j]=J[index][j];
pre[j]=index;
}
}
}
return ans;
} int cc(int s)
{
int MIN=INF, i, j;
for(i=; i<=n; i++)
for(j=i+; j<=n; j++)
{
if(!use[i][j] && J[i][j]!=INF)
{
MIN = min(MIN, s-Max[i][j]+J[i][j]);
}
}
return MIN;
} int main ()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, a, b, c;
scanf("%d%d", &n, &m); for(i=; i<=n; i++)
{
J[i][i]=;
for(j=; j<i; j++)
J[i][j]=J[j][i]=INF;
} for(i=; i<=m; i++)
{
scanf("%d%d%d", &a, &b, &c);
J[a][b]=J[b][a]=c;
} int ans1=prim();
int ans2=cc(ans1);
if(ans1==ans2)
printf("Not Unique!\n");
else
printf("%d\n", ans1);
}
return ;
}
(最小生成树 次小生成树)The Unique MST -- POJ -- 1679的更多相关文章
- The Unique MST POJ - 1679 (次小生成树)
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- The Unique MST POJ - 1679 次小生成树prim
求次小生成树思路: 先把最小生成树求出来 用一个Max[i][j] 数组把 i点到j 点的道路中 权值最大的那个记录下来 used数组记录该条边有没有被最小生成树使用过 把没有使用过的一条边加 ...
- 次小生成树 判断 unique MST
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- K - The Unique MST - poj 1679
题目的意思已经说明了一切,次小生成树... ****************************************************************************** ...
- Day5 - G - The Unique MST POJ - 1679
Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...
- The Unique MST POJ - 1679 最小生成树判重
题意:求一个无向图的最小生成树,如果有多个最优解,输出"Not Unique!" 题解: 考虑kruskal碰到权值相同的边: 假设点3通过边(1,3)连入当前所维护的并查集s. ...
- 训练指南 UVALive - 5713(最小生成树 + 次小生成树)
layout: post title: 训练指南 UVALive - 5713(最小生成树 + 次小生成树) author: "luowentaoaa" catalog: true ...
- POJ 1679 The Unique MST 【最小生成树/次小生成树模板】
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22668 Accepted: 8038 D ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题八 生成树 UVA 10600 ACM Contest and Blackout 最小生成树+次小生成树
题意就是求最小生成树和次小生成树 #include<cstdio> #include<iostream> #include<algorithm> #include& ...
随机推荐
- Hadoop主要架构
主要架构图 各部分作用 * Core:核心支持,内核代码 * MapReduce:映射数据 * HDFS:文件存储 * ZooKepper:服务器节点和进程通信的协调工具 * Pig:支持用户和Map ...
- spring中的BeanFactory和FactoryBean的区别与联系
首先,这俩都是个接口… 实现 BeanFactory 接口的类表明此类是一个工厂,作用就是配置.新建.管理 各种Bean. 而 实现 FactoryBean 的类表明此类也是一个Bean,类型为工厂B ...
- pipeenv error
[root@mhc tmp]# pipenv --python 3.6Creating a virtualenv for this project…Using /root/python3/bin/py ...
- ensembl数据库的使用方法
1)下载各种数据bam.gtf.fasta.ded等的地址 ftp://ftp.ensembl.org/../pub/release-93/
- OpenOffice Word文档转换成Html格式
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...
- TensorFlow—多层感知器—MNIST手写数字识别
1 import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import ...
- Windows phone Toast消息推送 学习笔记
简单介绍: Windows phone平台支持三种形式的推送通知: 1.Tile——也就是在Start屏幕程序平铺图标 2.Toast——创建一个显示在当前屏幕中的Toast弹出窗口 3.Raw——有 ...
- .net使用httpHandler添加图片防盗链
.net使用httpHandler添加图片防盗链1. 配置web.config: <!--图片添加水印的配置--> <httpHandlers> <add verb=&q ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- git设置别名alias
每次用git拉去版本库都很烦,特别是要从非origin源,非master分支, 例如 git pull gitlab mybranch ,这样很蛋疼. 1.写个sh去处理 2.可以通过git的别名设置 ...