第七届河南省赛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 ...
随机推荐
- OpenGL绘制环形渐变
开始看计算机图形学和OpenGL,挺有意思就自己随便写了一些效果. 以中间点坐标为圆心,计算每一点和圆心距离,根据距离算出一个RGB值,于是整体便呈现环形分布. 代码如下: #include < ...
- centos6.5编译android-2.2_froyo的几个问题jdk,gcc,arm-gcc
1.gcc降级 因为之前用QT升级了gcc到4.8.0,现在编译安卓又要降到4.4.6 我这边是直接下的gcc-4.4.6源码安装的 gcc源码安装包下载: ftp://ftp.mpi-sb.mpg. ...
- cad2007 钢筋符号显示为问号
如题:cad2007 钢筋符号显示为问号 解决办法:下载Tssdeng,解压(Tssdeng.rar) 把下载到的cad大字体Tssdeng.shx文件放到autoCAD2007安装目录C:\Prog ...
- 导出excel java实现
1.前台页面代码: <tr> <td><input dataId="excel" type="button" value=&quo ...
- spring的作用及优势---第一个spring示例
Spring 的作用及优势 * Spring 用于整合,好处是解耦. 解耦,可以降低组件不组件乊间的关联,改善程序结构,便于系统的维护和扩展. 我们在使用 Spring 框架时,主要是使用 Spri ...
- HDU 4664 Triangulation【博弈论】
一个平面上有n个点(一个凸多边形的顶点),每次可以连接一个平面上的两个点(不能和已经连接的边相交),如果平面上已经出现了一个三角形,则不能在这个平面上继续连接边了. 现在总共有N个平面,每个平面上都有 ...
- C++_enum
C++的enum可以限制成员的类型 //error C2440: “=”: 无法从“int”转换为“color” #include <iostream> using namespace s ...
- IOS系列——NStimer
Timer经常使用的一些东西 1. 初始化 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@select ...
- Python 读写文件操作
python进行文件读写的函数是open或file file_handler = open(filename,,mode) Table mode 模式 描述 r 以读方式打开文件,可读取文件信息. w ...
- IO库 8.3
题目:什么情况下,下面的while循环会终止? while(cin >> i) /* ... */ 解答:当读取发生错误时上述while循环会终止.比如i是整形,却输入非整形的数:输入文件 ...