【类克鲁斯卡尔做法+枚举最小边】【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]归程(克鲁斯卡尔重构树) 题面 洛谷 题解 我在现场竟然没有把这道傻逼题给切掉,身败名裂. 因为这题就是克鲁斯卡尔重构树的模板题啊 我就直接简单的说一下把 首先发现答案就是在只经过海 ...
随机推荐
- Swift中NSData与NSDictionary之间的相互转换
原创Blog,转载请注明出处 使用NSKeyedUnarchiver类来进行相互转换 1.NSDictionary转NSData var dictionaryExample : [String:Any ...
- unity 3d 生成apk文件时,设置图标
如图所示:注意红线标注部分: 如果安装到手机之后,程序的图标还是unity的默认图标,重启手机即可.
- 【进制问题】【HDU2056】A + B Again
A + B Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- zookeeper 丢失事件/miss event
今天在统计页面上发现有个节点丢失了,经过仔细分析后,发现同一个节点上的二个应用(同时监控zk)其中一个丢失了一个event,检查zk cluster没有发现异常... 通过网络搜寻,出现miss ev ...
- ASP.NET实现图片防盗链(转)
使用httpHandle来实现,对图片文件的请求做专门的处理第一步:创建一个类,继承自IHttpHandler,代码如下 C# code using System; using System.Web; ...
- hibernate报错:org.hibernate.MappingException: No Dialect mapping for JDBC type: -1
解决方法:自定义一个Hibernate Dialect. package com.yourcompany.util ; import java.sql.Types; import org.hibern ...
- PlSql复制角色、权限和添加角色权限
一.登录你想要复制数据库的用户
- JVM内存区域划分Eden Space、Survivor Space、Tenured Gen,Perm Gen解释
以下内容转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=29632145&id=4616836 jvm区域总体分两 ...
- java poi 导入excel
最近项目需要导入excel,网上有很多例子,自己整合记录下,兼容2003和2007,暂时没有添加图片处理功能. 所需jar包 http://pan.baidu.com/s/1sjPuWDR pack ...
- jQuery工具函数上
1.字符串操作 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...