第七届河南省赛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 ...
随机推荐
- 在IE6/7下表格td标签没有内容时不显示边框?
有以下几种方法: 1.在单元格中加入一个空格.这样: <td> </td> 2.直接在table里这样写:<table border="0" cell ...
- php数据类型有哪些?
php数据类型有哪些?有三大类1.基本数据类型 1.1整型 $a = 0123; // 八进制数(是以0开头) 83 $a = 0x1A; // 十六进制数 26 1.2小数 ...
- java 执行linux命令
原文地址: http://blog.csdn.net/xh16319/article/details/17302947 package scut.cs.cwh; import java.io.Inpu ...
- SQL Server Primary key 、clustered index 、 unique
primary key: 1.主键不可以有空值. 2.不可以有重复行. unique : 1.可以有空行. 2.不可以有重复行. clustered index: 1.可以有重复行. 2.可以有空行. ...
- Matlab 之meshgrid, interp, griddata 用法和实例
http://blog.sina.com.cn/s/blog_67f37e760101bu4e.html 实例结果http://wenku.baidu.com/link?url=SiGsFZIxuS1 ...
- libcurl编译
下载: git://github.com/bagder/curl.git openssl: openssl编译 for linux or mingw:./buildconf./configure ...
- [原]性能优化之Hibernate缓存讲解、应用和调优
近来坤哥推荐我我们一款性能监控.调优工具--JavaMelody,通过它让我觉得项目优化是看得见摸得着的,优化有了针对性.而无论是对于分布式,还是非分布,缓存是提示性能的有效工具. 数据层是EJB3. ...
- java 一致性哈希类实例 算法
package com.hash; import java.util.Collection; import java.util.SortedMap; import java.util.TreeMap; ...
- NOI2015 Day2
NOI2015 Day2 荷马史诗 题目描述:给出\(n\)个数,要求\(n\)个\(k\)进制数来对应这\(k\)个数(允许有前导零),\(n\)个\(k\)进制数互不为前缀,求\(n\)个数乘以对 ...
- 链表-Partition List
struct ListNode* partition(struct ListNode* head, int x) { struct ListNode *p1=(struct ListNode*)mal ...