【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】
题意:
/*
WA1:忘记不能到达输出-1 */
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#define oo 0x13131313
using namespace std;
struct Edge
{
int s,t,w;
};
int n,m;
Edge A[1100];
int father[300];
void init()
{
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
}
void input()
{
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&A[i].s,&A[i].t,&A[i].w);
}
}
void Clear()
{
for(int i=1;i<=299;i++)
father[i]=i;
}
bool cmp(Edge a,Edge b)
{
return a.w<b.w;
}
int find(int x)
{
if(x!=father[x])
father[x]=find(father[x]);
return father[x];
}
void Union(int a,int b)
{
int aa=find(a),bb=find(b);
father[aa]=bb;
}
void solve()
{
sort(A+1,A+m+1,cmp);
int Q,u,v; cin>>Q;
for(int k=1;k<=Q;k++)
{
cin>>u>>v;
int ok=0;
int ans=100000000;
for(int i=1;i<=m;i++)
{
Clear();
for(int j=i;j<=m;j++)
{
Union(A[j].s,A[j].t);
if(find(u)==find(v)) {
ans=min(ans,A[j].w-A[i].w);
break;
}
}
}
if(ans==100000000) printf("-1\n"); else printf("%d\n",ans);
}
}
int main()
{
// init();
while(cin>>n>>m)
{
input();
solve();
}
}
【类克鲁斯卡尔做法+枚举最小边】【HDU1598】【find the most comfortable road】的更多相关文章
- hdu1598 find the most comfortable road (枚举)+【并查集】
<题目链接> 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在 ...
- HDU1598 find the most comfortable road 【并查集】+【枚举】
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu1598 find the most comfortable road 枚举+最小生成树
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 210 #define IN ...
- [HDU1598]find the most comfortable road
思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...
- HDU-1598 find the most comfortable road
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 最舒适的路(并查集+枚举)(hdu1598)
hdu1598 find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768 ...
- 最小生成树之Kruskal(克鲁斯卡尔)算法
学习最小生成树算法之前我们先来了解下下面这些概念: 树(Tree):如果一个无向连通图中不存在回路,则这种图称为树. 生成树 (Spanning Tree):无向连通图G的一个子图如果是一颗包含G的所 ...
- 【BZOJ4242】水壶(克鲁斯卡尔重构树,BFS)
[BZOJ4242]水壶(克鲁斯卡尔重构树,BFS) 题面 BZOJ然而是权限题. Description JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长 ...
- 【BZOJ5415】【NOI2018】归程(克鲁斯卡尔重构树)
[NOI2018]归程(克鲁斯卡尔重构树) 题面 洛谷 题解 我在现场竟然没有把这道傻逼题给切掉,身败名裂. 因为这题就是克鲁斯卡尔重构树的模板题啊 我就直接简单的说一下把 首先发现答案就是在只经过海 ...
随机推荐
- openssl 生成CSR
openssl 生成CSR 2013-12-27 15:05 3699人阅读 评论(1) 收藏 举报 分类: Security(38) C/C++(105) 版权声明:本文为博主原创文章,未经博 ...
- linux 修改文件、文件夹权限
# change owner of all the fies under dirName chown -R username dirName #change owner and the file gr ...
- m元素集合的n个元素子集
理论: 假设有5个元素的集点,取出3个元素的可能子集如下:{1 2 3}.{1 2 4 }.{1 2 5}.{1 3 4}.{1 3 5}.{1 4 5}.{2 3 4}.{2 3 5}.{2 4 5 ...
- android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题
android createbitmap函数内存溢出,求解怎样进行处理out of memory溢出问题 android createbitmap函数内存溢出,求解怎样进行处理out of memor ...
- 谋哥:研究App排行榜浮出的神器
昨天发的<App排行榜的秘密>到头条网,阅读量到2万,踩的比顶的多几倍.原因是由于我使用360市场的数据来分析,而且这帮喷子根本不看你分析数据背后的意义,反正看到自己不喜欢的比方" ...
- 说说ShellExecuteEx
今天来说说ShellExecuteEx这个函数,先翻译MSDN,然后看个样例. ShellExecuteEx Function 对指定应用程序运行某个操作 语法: BOOL ShellExecuteE ...
- 【Java基础】单例模式
单例模式:保证整个项目运作期间某一个对象有且只有一个. 主要有:1,饿汉式:2,懒汉式. class Singleton1//饿汉式 { private Singleton1(){}//先把构造方法私 ...
- Python和C#基本算法实现对比
最近在学习python,很多入门的例子又写了一遍,基本上是C#和Python都写了一遍,对比发现语言真是相通啊,只是语法不同而已. python开发也是用的VS,很好用,特别是代码段运行,选中一段py ...
- Win7 64位系统上配置使用32位的Eclipse(转)
Win7 64位系统上配置使用32位的Eclipse 博客分类: Eclipse eclipse 最近工作电脑换成了64位的win7系统,之前个人电脑上安装的jdk和Eclipse都是32位的.而新 ...
- HDU 1194 - Beat the Spread!
给两数之和和两数之差,求两数,两数还必须同奇偶 #include <iostream> using namespace std; int main() { int a,b,t; cin&g ...