https://www.bnuoj.com/v3/contest_show.php?cid=9146#problem/F

【题意】

给定n个城市和m条带权边,q次查询,问某两个城市之间的所有路径中最大边和最小边的差值最小是多少?

【思路】

首先给所有的边从小到大排序,然后枚举最小边,向右遍历各条边:查找这条边的两个端点是否连通,不连通就加入并查集,并且更新q个查询的答案;连通的话不进行任何操作

【Accetped】

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring> using namespace std;
typedef long long ll;
typedef double db;
const int maxm=1e3+;
const int inf=0x3f3f3f3f;
struct sars
{
int x;
int y;
int w;
bool operator<(const sars& t)const
{
return w<t.w;
}
}node[maxm];
int n,m,q;
const int maxn=;
int stt[],ed[],ans[];
int fa[maxn];
int getfa(int x)
{
return x==fa[x]?x:fa[x]=getfa(fa[x]);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(ans,inf,sizeof(ans));
for(int i=;i<m;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
}
sort(node,node+m);
scanf("%d",&q);
for(int i=;i<q;i++)
{
scanf("%d%d",&stt[i],&ed[i]);
}
for(int i=;i<m;i++)
{
for(int k=;k<=n;k++)
{
fa[k]=k;
}
int mns=node[i].w;
for(int k=i;k<m;k++)
{
int u=node[k].x;
int v=node[k].y;
int w=node[k].w;
int x=getfa(u);
int y=getfa(v);
if(x!=y)
{
fa[x]=y;
for(int j=;j<q;j++)
{
if(getfa(stt[j])==getfa(ed[j]))
{
ans[j]=min(ans[j],w-mns);
}
}
}
}
}
for(int i=;i<q;i++)
{
if(ans[i]==inf)
{
printf("-1\n");
}
else
{
printf("%d\n",ans[i]);
}
}
}
return ;
}

【并查集】F.find the most comfortable road的更多相关文章

  1. 并查集-F - How Many Tables

    F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...

  2. poj1456 Supermarket[另类的并查集做法]

    1.Supermarket(题目地址) 跟很久以前模拟的打地鼠那题一样,贪心+优先队列.这次换用并查集做法. 还是基于贪心,但这次换一种策略,先选价值最大的, 同时使其尽可能晚的被选上(因为早选会将之 ...

  3. 洛谷 - P1552 - 派遣 - 左偏树 - 并查集

    首先把这个树建出来,然后每一次操作,只能选中一棵子树.对于树根,他的领导力水平是确定的,然后他更新答案的情况就是把他子树内薪水最少的若干个弄出来. 问题在于怎么知道一棵子树内薪水最少的若干个分别是谁. ...

  4. B. Mr. Kitayuta's Colorful Graph,二维并查集,一个简单变形就可以水过了~~

    B. Mr. Kitayuta's Colorful Graph ->  Link  <- 题目链接在上面,题目比较长,就不贴出来了,不过这是道很好的题,很多方法都可以做,真心邀请去A了这 ...

  5. 洛谷 P1892 [BOI2003]团伙(种类并查集)

    传送门 解题思路 用并查集f存朋友关系,一个数组e存的是敌人关系,是一个辅助数组,所以叫做种类并查集. 当p和q是朋友时,直接合并,但是当是敌人时,需要一些操作. 当p还没有敌人时(即p的敌人是自己) ...

  6. hdu 1598 find the most comfortable road(并查集+枚举)

    find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  7. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  8. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集

    题目链接: 题目 F. Polycarp and Hay time limit per test: 4 seconds memory limit per test: 512 megabytes inp ...

  9. F - True Liars - poj1417(背包+并查集)

    题意:有这么一群人,一群好人,和一群坏人,好人永远会说实话,坏人永远说假话,现在给你一组对话和好人与坏人的数目P1, P2. 数据里面的no是A说B是坏人, yes代表A说B是好人,就是这样,问题能不 ...

随机推荐

  1. CentOS6.5下安装Redis2.8.6和phpredis2.2.4扩展

    一.版本说明 CentOS版本 [plain]view plaincopyprint? [root@localhost ~]# uname Linux [root@localhost ~]# unam ...

  2. vs2013转为vs2010项目

    1.首先用记事本之类的工具打开.sln文件 打开后会看到如下信息 Format Version 12.00 就是指VS2013 VisualStudioVersion = 12.0.21005.1 指 ...

  3. PL/SQL笔记(1)-流程控制,循环,异常,块

    流程控制 1.If,then,else,elsif(不是elseif) ' then null; endif; 2.Case 简单case表达式: 搜索型Case表达式: 3.goto语句 begin ...

  4. Android 使用GridView+仿微信图片上传功能(附源代码)

    由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...

  5. APM系列-国外新兴厂商New Relic vs. AppDynamics

    前: New Relic的上市使得IT和资本界开始重新重视APM,当然跟传统APM相比,New Relic还是有相当的创新,另外还有一点是目前的创业潮导致的企业级需求增大. In recent yea ...

  6. 11G GI启动顺序

    --11gR2 Clusterware and Grid Home - What You Need to Know (文档 ID 1053147.1)         上图来自<Oracle C ...

  7. Spread / Rest 操作符

    Spread / Rest 操作符指的是 ...,具体是 Spread 还是 Rest 需要看上下文语境. 当被用于迭代器中时,它是一个 Spread 操作符:(参数为数组) function foo ...

  8. 【转】Delphi 文件读写

    procedure TForm1.Button1Click(Sender: TObject); variFileHandle: Integer;iFileLength: Integer;iBytesR ...

  9. Chrome安装助手踩坑

    [前言] 最近用之前的方法配置hosts,想浏览下载国外网站的数据和插件,突然发现几乎所有的方法都无效了...... 本文介绍下下载谷歌助手,通过助手访问国外网站 [主体] (1)搜索谷歌助手,点击下 ...

  10. Oracle中的for和while循环

    实例: beginfor i in 51..500 loop delete from test t where t.date=to_date('2016-07-01', 'yyyy-MM-dd') a ...