第七届河南省赛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 ...
随机推荐
- javscript上传图片前预览的方法setPreViewImage()
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- myeclipse 保存时自动格式化代码
windows -> preferences -> Java -> Editor -> Save Actions... 这就可以用到很多功能了,可以自己定义一些个保存后要处理的 ...
- jq方法
DOM属性-获取和设置页面元素的DOM属性 .addClass()..attr()..prop()..hasClass()..html()..removeAttr()..removeClass().. ...
- Android模拟器使用笔记,学习head_first python 安卓开发章节
学习head_first python 安卓开发那一章需要的程序android-sdk_r23.0.2-windows.zip //模拟器 PythonForAndroid_r4.apk sl4a_r ...
- 初识C(2)---从printf函数开始
继承[K&R]的传统,我们的第一个C语言程序也是“Hello, World.”. 书写C语言程序的大前提:C语言中的语法符号必须都是英文字符,即在中文输入法关闭状态下输入的字符. 例 1. H ...
- linux note
用 &&组合两个命令,比如: cd dir && ls
- Windows Azure 社区新闻综述(#71 版)
欢迎查看最新版本的每周综述,其中包含有关云计算和 Windows Azure的社区推动新闻.内容和对话. 以下是过去一周基于您的反馈汇集在一起的内容: 文章.视频和博客文章 · 使用 Azure ...
- c# 搭建服务端 常用的Helper(5)
常用的Helper 1.byteHelper :对象与byte[]之间的转换 2.ConvertJson:操作json对象 3.EncodingHelper:对象编码 4.ModelConvertHe ...
- Android TXT文件读写
package com.wirelessqa.helper; import java.io.FileInputStream; import java.io.FileOutputStream; impo ...
- c++ 回调类成员函数实现
实现类成员函数的回调,并非静态函数:区分之 #ifndef __CALLBACK_PROXY_H_ #define __CALLBACK_PROXY_H_ template <typename ...