POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29902 | Accepted: 10697 |
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
triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
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!
Source
#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
#include<cstring>
using namespace std;
#define LL long long struct node
{
int u,v,w,flag;
} p[100005]; int n,m,cnt,tot,pre[1005],f[100005]; bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<1004; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} int kruskal(int xx)
{ init();
int cost=0;
int ans=0;
int flg=0;
for(int i=0; i<cnt; i++)
{
if(f[i])
continue;
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
if(xx==1)
p[i].flag=1;
}
}
if(ans==n-1)
return cost;
else
return -1;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(p,0,sizeof p);
memset(f,0,sizeof f);
scanf("%d%d",&n,&cnt);
for(int i=0; i<cnt; i++)
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
sort(p,p+cnt,cmp);
int mintree=kruskal(1);
int sectree=999999999;
for(int i=0; i<cnt; i++)
{
if(p[i].flag==1)
{
f[i]=1;
int ans=kruskal(0);
if(ans!=-1)
sectree=min(sectree,ans);
f[i]=0;
}
}
if(sectree==mintree)
printf("Not Unique!\n");
else
printf("%d\n",mintree);
}
return 0;
}
POJ1679 The Unique MST 2017-04-15 23:34 29人阅读 评论(0) 收藏的更多相关文章
- Codeforces761B Dasha and friends 2017-02-05 23:34 162人阅读 评论(0) 收藏
B. Dasha and friends time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- HDU2033 人见人爱A+B 分类: ACM 2015-06-21 23:05 13人阅读 评论(0) 收藏
人见人爱A+B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- ZOJ2481 Unique Ascending Array 2017-04-18 23:08 33人阅读 评论(0) 收藏
Unique Ascending Array Time Limit: 2 Seconds Memory Limit: 65536 KB Given an array of integers ...
- PAT甲 1041. Be Unique (20) 2016-09-09 23:14 33人阅读 评论(0) 收藏
1041. Be Unique (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Being uniqu ...
- Uniform Generator 分类: HDU 2015-06-19 23:26 11人阅读 评论(0) 收藏
Uniform Generator Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- pascal矩阵 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏
帕斯卡矩阵 1.定义 帕斯卡矩阵:由杨辉三角形表组成的矩阵称为帕斯卡(Pascal)矩阵. 杨辉三角形表是二次项 (x+y)^n 展开后的系数随自然数 n 的增大组成的一个三角形表. 如4 ...
- MATLAB格式化输出控制 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏
MATLAB格式化输出控制 format 默认格式 format short 5字长定点数 format long 15字长定点数 format short e 5字长浮点数 format long ...
- NYOJ-235 zb的生日 AC 分类: NYOJ 2013-12-30 23:10 183人阅读 评论(0) 收藏
DFS算法: #include<stdio.h> #include<math.h> void find(int k,int w); int num[23]={0}; int m ...
随机推荐
- 黑马Python——学习之前
学习清单 学习态度
- Tomcat 容器的设计和实现
Tomcat 容器是对 Servlet 规范的实现,也称为 Servlet 引擎.在分析 Tomcat 容器的设计和实现之前,首先简单了解一下 Servlet 规范,弄清楚 Tomcat 究竟要实现什 ...
- 关于centos6升级python3.6无法使用pip的问题
用find / -name "pip",也找不到类似/usr/bin/pip类似的命令,说明pip没安装好,那么运行下面的命令 yum install python-pip 然后可 ...
- ubuntu 下出现E: Sub-process /usr/bin/dpkg returned an error code
在用apt-get安装软件时出现了类似于 install-info: No dir file specified; try –help for more information.dpkg:处理 get ...
- Oracle数据库LOGGING&NOLOGGING模式概述
1.日志记录模式(LOGGING .FORCE LOGGING .NOLOGGING) 1.1三者的含义 LOGGING:当创建一个数据库对象时将记录日志信息到联机重做日志文件.LOGGING实际上是 ...
- JS中的this机制
1.this是啥? 简言之,this是JavaScript语言中定义的众多关键字之一,它的特殊在于它自动定义于每一个函数域内,但是this倒地指引啥东西却让很多人张二摸不着头脑.这里我们留个小悬念,希 ...
- springBatch学习 batch的使用方式(5)
首先讲一下batch框架提供的一组job执行的api 如下图 说明: 应用场景 包含三种 标准的web ,定时任务调度,命令行 1.命令行 通过命令行在单独的jvm中调用 进行批处理作业 spring ...
- 31_java之项目中的数据库操作
01项目训练目标 * A: 项目训练目标 * a: 项目目标 * 综合运用前面所学习的知识点 * 熟练View层.Service层.Dao层之间的方法相互调用操作. * 熟练dbutils操作数据库表 ...
- ES6系列_15之class类的使用
JS语言的传统方法是通过构造函数,定义并生成新对象,是一种基于原型的面向对象系统.在ES6中新增加了类的概念,可以使用 class 关键字声明一个类,之后以这个类来实例化对象.1.先来看看es5与es ...
- c#二维码建立与识别
QrCodeEncodingOptions options = new QrCodeEncodingOptions(); options.CharacterSet = "UTF-8" ...