第七届河南省赛B.海岛争霸(并差集)
B.海岛争霸
Time Limit: 2 Sec Memory Limit: 128 MB Submit: 130 Solved: 48 [Submit][Status][Web Board]
Description
Input
Output
Sample Input
10 8
1 2 5
1 3 2
2 3 11
2 4 6
2 4 4
6 7 10
6 10 5
10 7 2
5
2 3
1 4
3 7
6 7
8 3
Sample Output
5
5
-1
5
-1
HINT
Source
题解:并差集;让找最小的最大危险系数;
暴力,从0到m假设每个为起点,加边,当加到两个点联通时,找最小值;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
int N;
const int MAXN=110;
int pre[MAXN];
int find(int x){
return pre[x]=pre[x]==x?x:find(pre[x]);
//return x==pre[x]?x:pre[x]=find(pre[x]);
}
void init(){
for(int i=1;i<N;i++)pre[i]=i;
}
void merge(int x,int y){
int f1=find(x),f2=find(y);
if(f1!=f2)pre[f1]=f2;
}
struct Node{
int u,v,w;
}dt[MAXN];
int cmp(Node a,Node b){
return a.w<b.w;
}
int main(){
int M,Q,a,b,c,temp,ans,flot;
SI(N);SI(M);
for(int i=0;i<M;i++)
scanf("%d%d%d",&dt[i].u,&dt[i].v,&dt[i].w);
sort(dt,dt+M,cmp);
SI(Q);
while(Q--){
scanf("%d%d",&a,&b);
ans=INF;
for(int i=0;i<M;i++){
init();
temp=0;
flot=0;
for(int j=i;j<M;j++){
merge(dt[j].u,dt[j].v);
temp=max(temp,dt[j].w);
if(find(a)==find(b)){
flot=1;
break;
}
}
if(flot)ans=min(ans,temp);
}
if(ans==INF)puts("-1");
else printf("%d\n",ans);
}
return 0;
}
第七届河南省赛B.海岛争霸(并差集)的更多相关文章
- 第七届河南省赛10403: D.山区修路(dp)
10403: D.山区修路 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 69 Solved: 23 [Submit][Status][Web Bo ...
- 第七届河南省赛10402: C.机器人(扩展欧几里德)
10402: C.机器人 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 53 Solved: 19 [Submit][Status][Web Boa ...
- 第七届河南省赛G.Code the Tree(拓扑排序+模拟)
G.Code the Tree Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 18 [Submit][Status][Web ...
- 第七届河南省赛A.物资调度(dfs)
10401: A.物资调度 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 95 Solved: 54 [Submit][Status][Web Bo ...
- 第七届河南省赛H.Rectangles(lis)
10396: H.Rectangles Time Limit: 2 Sec Memory Limit: 128 MB Submit: 229 Solved: 33 [Submit][Status] ...
- 第七届河南省赛F.Turing equation(模拟)
10399: F.Turing equation Time Limit: 1 Sec Memory Limit: 128 MB Submit: 151 Solved: 84 [Submit][St ...
- 山东省第七届省赛 D题:Swiss-system tournament(归并排序)
Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...
- poj 2567 Code the Tree 河南第七届省赛
Code the Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2350 Accepted: 906 Desc ...
- 第六届河南省赛 River Crossing 简单DP
1488: River Crossing Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 42 SubmitStatusWeb ...
随机推荐
- php数组排序和分割字符串
function sortStr($str){ $ary = str_split($str); sort($ary); $len = count($ary); $arr = array(); for( ...
- js 获取月份 格式yy-mm-dd
/** * 获取上一个月 * * @date 格式为yyyy-mm-dd的日期,如:2014-01-25 */ function getPreMonth(date) { var arr = date. ...
- background:url 的使用方法
#pingfen li{ width:27px; float:left; height:28px; cursor:pointer; background:url( ; list-style:none; ...
- 图的邻接链表实现(c)
参考:算法:C语言实现 一书 实现: #ifndef GRAPH #define GRAPH #include<stdio.h> #include<stdlib.h> stru ...
- SQL Server一些重要视图 1
第一个: sys.indexs 每个堆与索引在它上有一行. 第二个: sys.partitions每个堆与索引的每一个分区返回一行.每一张表最多可以有1000个区. 第三个: sys. allocat ...
- 第五章 Spring3.0 、Hibernate3.3与Struts2的整合
5.1整合Spring与Hibernate 5.1.1使用MyEclipse加入Spring与Hibernate功能 使用MyEclipse工具主要是为了让工程拥有把数据表生成实体类与映射的功能.然后 ...
- poj2823_单调队列简单入门
题目链接:http://poj.org/problem?id=2823 #include<iostream> #include<cstdio> #define M 100000 ...
- [LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 解决cookie无法删除的问题
今天遇到一个cookie无法删除的问题,退出操作时cookie无法删除,必须在首页先进行退出操作,后来发现一个网友的博客,介绍了无法删除Cookie的原因,原来是我关于cookie的基础知识没搞清楚. ...
- SPOJ 3267 求区间不同数的个数
题意:给定一个数列,每次查询一个区间不同数的个数. 做法:离线+BIT维护.将查询按右端点排序.从左到右扫,如果该数之前出现过,则将之前出现过的位置相应删除:当前位置则添加1.这样做就保证每次扫描到的 ...