hdu1598 find the most comfortable road 枚举+最小生成树
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define MAXN 210
- #define INF 2147483646
- using namespace std;
- int f[MAXN], Rank[MAXN]; //Rank长度
- int n, m, pos;
- struct Edge{
- int u, v, val;
- //按照val从小到大排列
- friend bool operator<(const Edge&a, const Edge&b){
- return a.val < b.val;
- }
- }arr[MAXN*MAXN];
- void init(){
- for (int i = ; i<MAXN; ++i)
- f[i] = i, Rank[i] = ;
- }
- int find(int x){
- int i, j = x;
- while (j != f[j]) j = f[j];
- while (x != j){
- i = f[x]; f[x] = j; x = i;
- }
- return j;
- }
- void Union(int x, int y){
- int a = find(x), b = find(y);
- if (a == b)return;
- if (Rank[a]>Rank[b])
- f[b] = a;
- else{
- if (Rank[a] == Rank[b])
- ++Rank[b];
- f[a] = b;
- }
- }
- int main(){
- int u, v, speed, Q;
- while (scanf("%d%d", &n, &m) != EOF){
- for (int i = ; i<m; ++i)
- scanf("%d%d%d", &arr[i].u, &arr[i].v, &arr[i].val);
- sort(arr, arr + m);
- scanf("%d", &Q);
- while (Q--){
- scanf("%d%d", &u, &v);
- int ans = INF;
- for (int j = ; j < m; ++j){
- init();
- for (int k = j; k < m; ++k){
- Union(arr[k].u, arr[k].v);
- if (find(u) == find(v)){
- ans = min(ans, arr[k].val - arr[j].val); //使ans尽可能的小
- break;
- }
- }
- }
- if (ans == INF)
- printf("-1\n");
- else
- printf("%d\n", ans);
- }
- }
- //system("pause");
- return ;
- }
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 ...
- hdu 1598 find the most comfortable road(枚举+卡鲁斯卡尔最小生成树)
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU-1598 find the most comfortable road
find the most comfortable road Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)
一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...
- HDU 1598 find the most comfortable road (最小生成树) >>
Problem Description XX明星有许多城市,通过与一个陌生的城市高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流.每条SARS都对行驶 ...
- [HDU1598]find the most comfortable road
思路: 考虑一个暴力:枚举最大的边权和最小的边权,然后将边权在这之间的边全拿出来构成一张无向图,剩下的就是判断是否存在一条从$S$到$T$的路径.相当于判$S$和$T$是否连通,用并查集连一下即可.时 ...
- hdu-1598 find the most comfortable road---kruskal+枚举下界
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 题目大意: XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Ro ...
- HDU - 1598 find the most comfortable road 【最小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1598 思路 用kruskal 算法 将边排序后 跑 kruskal 然后依次将最小边删除 再去跑 kr ...
随机推荐
- spring test---測试SpringMvc初识
如今越来越多人使用SpringMvc来开发系统,在开发中可定须要对后台url地址请求測试,而且返回预期的结果! Spring提供的測试类MockMvc来进行url地址请求測试,使用方方式: packa ...
- sanic官方文档解析之Example(一)
1,示例 这部的文档是简单的示例集合,它能够帮助你快速的启动应用大部分的应用,这些应用大多事分类的,并且提供给ini工作的连接代码: 1.1,基础示例 这部分示例集成了提供简单sanic简单的代码 单 ...
- SequenceFileInputFormat区别TextInputFormat
通过InputFormat,Hadoop可以: l 检查MapReduce输入数据的正确性: l 将输入数据切分为逻辑块InputSplit,这些块会分配给Ma ...
- 常见mysql分布式数据中间件
一般分为三种: 1. proxy sharding,目前由cobar,mycat,drds,atlas修改,这几个产品的起源一般是mysqlproxy 或 ameoba,特点是mysql协议基本兼容, ...
- SyntaxError:Strict mode does not allow function declaration in a lexically nested statement.
问题描述 使用react-native init创建了一个新项目,在package.json中使用的react-native的版本如下: "dependencies": { & ...
- ICMP协议 广播以查询局域网内的所有主机
看到了很多局域网内的主机扫描工具,在想怎么去实现这样一个工具.前几天看了Ping源码--ICMP协议的实例,ICMP可以用来探测网联网内的任一主机,ICMP和广播地址结合来扫描局域网内的所有主机不是很 ...
- extjs4.0 treepanel节点的选中、展开! 数据的重新加载
1.extjs4.0API较3.0有非常大变化2.多级子父节点的选中和展开.3.数据的重新加载.tree.getStore().load4.节点的移除,从树中根据ID获取节点 tree.getStor ...
- Why you shouldn’t connect your mobile application to a database
BY CRAIG CHAPMAN · PUBLISHED 2015-07-02 · UPDATED 2015-07-02 Working at Embarcadero, I frequently ...
- css3中animation的应用
1.css3 的相关属性: 相关代码: div { animation-name: myfirst; //动画的名称 animation-duration: 5s; //动画一个周期需要5秒 anim ...
- CentOS7 安装和配置 mysql5.7
1.下载 mysql源安装包 wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2.安装mysql源 ...