POJ1679(次小生成树)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 24201 | Accepted: 8596 |
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!
题意:判断是否存在唯一的最小生成树。
思路:先求出一个最小生成树。在这颗树上先加入(x,y),加入后一定会成环,如果删除(x,y)以外最大的一条边,会得到加入(x,y)时权值最小的一棵树。如果加入的边和删除的边相等,则最小生成不是唯一的。
收获:了解了次小生成树。
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
#define maxn 500
int n, m;
int a[maxn][maxn];
int used[maxn][maxn];
int mmax[maxn][maxn];
int vis[maxn];
int dis[maxn];
int pre[maxn];
int prim()
{
int ans = ;
memset(vis, , sizeof vis);
memset(used, , sizeof used);
memset(mmax, ,sizeof mmax);
for(int i = ; i <= n; i++)
{
dis[i] = a[][i];
pre[i] = ;
}
vis[] = ;
dis[] = ;
for(int i = ; i < n; i++)
{
int temp = INF;
int k = -;
for(int j = ; j <= n; j++)
{
if(!vis[j] && temp > dis[j])
{
temp = dis[j];
k = j;
}
}
if(k == -) return ans;
ans += temp;
vis[k] = ;
used[k][pre[k]] = used[pre[k]][k] = ;
mmax[pre[k]][k] = temp;
for(int j = ; j <= n; j++)
mmax[j][k] = max(mmax[j][pre[k]],mmax[pre[k]][k] );//找最大的边权的边。
for(int j = ; j <= n; j++)
{
if(!vis[j] && dis[j] > a[k][j])
{
dis[j] = a[k][j];
pre[j] = k;
}
}
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
int u, v, w;
memset(a, INF, sizeof a);
for(int i = ; i < m; i++)
{
scanf("%d%d%d", &u, &v, &w);
a[u][v] = a[v][u] = w;
}
int mst = prim();
int flag = ;
for(int i = ; i <= n; i++)
for(int j = i+; j <= n; j++)
{
if(a[i][j] == INF || used[i][j] == )
continue;
if(a[i][j] == mmax[i][j])//判断加入的边和删除的边是否相等。
{
flag = ;
break;
}
}
if(flag)
printf("Not Unique!\n");
else
printf("%d\n",mst);
}
return ;
}
POJ1679(次小生成树)的更多相关文章
- 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 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36692 Accepted: 13368 ...
- 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 ...
随机推荐
- CentOS下安装无线网卡驱动 (转)
1. 确定自己的网卡和内核版本:lspci | grep Network #根据输出的信息确定网卡的型号.uname -a #确定内核版本 2. 配置yum使用RPMForg ...
- HOJ 1096 Divided Product (DFS)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Given two positive integers N and M, please divide N into sev ...
- The 4th tip of DB Query Analyzer
The 4th tip of DB QueryAnalyzer Ma Genfeng (Guangdong Unitoll Services incorporated, Guangzhou 51030 ...
- setOpaque(true);设置控件不透明
setOpaque(true);设置控件不透明setOpaque(false);设置控件透明
- Android开源库集锦
一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...
- 计算直线的交点数(set + 打表)
计算直线的交点数 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- According to TLD or attribute directive in tag file, attribute value does not accept any expressions
1.错误描写叙述 2014-7-13 17:27:21 org.apache.jasper.compiler.TldLocationsCache tldScanJar 信息: At least one ...
- java集合总结【转】
Map.Set.Iterator迭代详解 Map接口定义了四种类型的方法,每个Map都包含这些方法. equals(Object o)比较指定对象与此Map的等价性. hashCode()返回此Map ...
- sql语句中查询出的数据添加一列,并且添加默认值
查询出数据,并且要添加一列表中都不存在的数据,且这一列的值都是相等的 select app_id,app_secret from wx_ticket group by app_id; 查询出的数据是 ...
- python os模块文件相关
使用前 import os导入模块 os模块: os.sep 可以取代操作系统特定的路径分割符 os.linesep 字符串给出当前平台使用的行终止符.例如,Windows使用'\r\n ...