Description

给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000)。给你两个顶点S和T,求一条路径,使得路径上最大边和最小边的比值最小。如果S和T之间没有路径,输出”IMPOSSIBLE”,否则输出这个比值,如果需要,表示成一个既约分数。 备注: 两个顶点之间可能有多条路径。

Input

第一行包含两个正整数,N和M。 下来的M行每行包含三个正整数:x,y和v。表示景点x到景点y之间有一条双向公路,车辆必须以速度v在该公路上行驶。 最后一行包含两个正整数s,t,表示想知道从景点s到景点t最大最小速度比最小的路径。s和t不可能相同。

Output

如果景点s到景点t没有路径,输出“IMPOSSIBLE”。否则输出一个数,表示最小的速度比。如果需要,输出一个既约分数。

Sample Input

【样例输入1】
4 2
1 2 1
3 4 2
1 4
【样例输入2】
3 3
1 2 10
1 2 5
2 3 8
1 3
【样例输入3】
3 2
1 2 2
2 3 4
1 3

Sample Output

【样例输出1】
IMPOSSIBLE
【样例输出2】
5/4
【样例输出3】
2

HINT

【数据范围】
1<  N < = 500
1 < = x, y < = N,0 < v < 30000,x ≠ y
0 < M < =5000

Source

【分析】

凑数题。

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <map> const int MAXN = + ;
const int MAXM = + ;
using namespace std;
struct EDGE{
int u, v ,w;
bool operator < (EDGE B)const{
return w < B.w;
}
}edge[MAXM];
int n, m, parent[MAXN];
int s, t; int find(int x){return parent[x] < ? x:parent[x] = find(parent[x]);}
void merge(int x, int y){
if (parent[x] > parent[y]){
parent[y] += parent[x];
parent[x] = y;
}else{
parent[x] += parent[y];
parent[y] = x;
}
}
void init(){
scanf("%d%d", &n, &m);
memset(parent, -, sizeof(parent));
for (int i = ; i <= m; i++){
scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
}
scanf("%d%d", &s, &t);
sort(edge + , edge + + m);
}
int gcd(int a, int b){return b == ? a:gcd(b, a % b);}
void work(){
int A = , B = ;
//枚举边长最小的边
for (int i = ; i <= m; i++){
memset(parent, -, sizeof(parent));
int j;
for (j = i; j <= m; j++){
int u = edge[j].u, v = edge[j].v;
u = find(u); v = find(v);
if (u != v) merge(u, v);
if (find(s) == find(t)) break;
}
if (j <= m){
if (edge[i].w * A > edge[j].w * B){
A = edge[j].w;
B = edge[i].w;
}
}
}
if (A == && B == ) {printf("IMPOSSIBLE\n");return;}
if (A % B == ) printf("%d\n", A / B);
else printf("%d/%d", A / gcd(A, B), B / gcd(A, B));
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
init();
work();
return ;
}

【BZOJ1050】【枚举+并查集】旅行comf的更多相关文章

  1. BZOJ 1050: [HAOI2006]旅行comf(枚举+并查集)

    [HAOI2006]旅行comf Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点 ...

  2. bzoj 1050: [HAOI2006]旅行comf【枚举+并查集】

    m是5000,就想到了直接枚举比例 具体做法是是先把边按照边权从小到大排序,然后先枚举最小边权,再枚举最大边权,就是从最小边权里一个一个加进并查集里,每次查st是否联通,联通则退出,更新答案 #inc ...

  3. POJ 1944 Fiber Communications (枚举 + 并查集 OR 线段树)

    题意 在一个有N(1 ≤ N ≤ 1,000)个点环形图上有P(1 ≤ P ≤ 10,000)对点需要连接.连接只能连接环上相邻的点.问至少需要连接几条边. 思路 突破点在于最后的结果一定不是一个环! ...

  4. bzoj 4078: [Wf2014]Metal Processing Plant【二分+2-SAT+枚举+并查集】

    枚举从大到小s1,二分s2(越大越有可能符合),2-SAT判断,ans取min 思路倒是挺简单的,就是二分的时候出了比较诡异的问题,只能二分s2的值,不能在数组上二分... 有个优化,就是当不是二分图 ...

  5. nyoj 711 枚举+并查集

     #include<stdio.h>//从大到小不断枚举边直到找到s-t的路径,判断从s可以到t可以用并查集来判断 #include<stdlib.h>//枚举最大的一条边肯定 ...

  6. SGU 128. Snake --- 暴力枚举+并查集+贪心+计算几何

    <传送门> 128. Snake time limit per test: 0.25 sec. memory limit per test: 4096 KB There are N poi ...

  7. [BZOJ1050][HAOI2006]旅行comf 枚举+并查集

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1050 将边排序,枚举边权最小的边,依次加边直到S和T连通,更新答案. #include&l ...

  8. HDU 1598 find the most comfortable road(枚举+并查集,类似于最小生成树)

    一开始想到用BFS,写了之后,发现有点不太行.网上查了一下别人的解法. 首先将边从小到大排序,然后从最小边开始枚举,每次取比它大的边,直到start.end属于同一个集合,即可以连通时停止.过程类似于 ...

  9. BZOJ 1050 枚举+并查集

    思路: 枚举最大边 像Kruskal一样加边 每回更新一下 就搞定了- //By SiriusRen #include <cstdio> #include <cstring> ...

随机推荐

  1. MYSQL仅仅向某个字段进行插入

    例子: mysql> show create table student \G . row *************************** Table: student Create T ...

  2. HDU-4974 A simple water problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4974 话说是签到题,我也不懂什么是签到题. A simple water problem Time Limit: ...

  3. Supervisord管理

    原文地址:http://blog.csdn.net/fyh2003/article/details/6837970 学习笔记 Supervisord可以通过sudo easy_install supe ...

  4. JavaScript高级程序设计18.pdf

    系统对话框 alert().confirm()和prompt()调用系统对话框向用户显示消息,显示对话框的时候代码会停止执行,关闭后继续执行 alert()警告提示框 confirm()点击确认返回t ...

  5. Spark SQL编程指南(Python)

    前言   Spark SQL允许我们在Spark环境中使用SQL或者Hive SQL执行关系型查询.它的核心是一个特殊类型的Spark RDD:SchemaRDD.   SchemaRDD类似于传统关 ...

  6. windows下安装pip

    1.在安装pip前,请确认win系统中已经安装好了python,和easy_install工具,如果系统安装成功,easy_install在目录C:\Python27\Scripts 下面,确认截图如 ...

  7. html5之canvas困惑 在canvas标签内需要设置了宽跟高,如果在css中设置同样的宽跟高,画出来的图像变形了?

    <canvas class="cvs"></canvas> 遇到的问题: 如css 中设.cvs{width:500px;height:400px;},也就 ...

  8. CBO学习----03--选择率(Selectivity)

    第3章 单表选择率(Single Table Selectivity) Selectivity是优化器估算Rows(Cards)的重要依据. /**************************** ...

  9. 未能正确加载“visual C++ package”包

    早上打开360要卸载软件,跳出说系统修复,习惯性的点击修复,结果修复后发现打开vs2012提示“未能正确加载“visual C++ package”包……..”, 重启也一样,google了下,是因为 ...

  10. ※C++随笔※=>☆C++基础☆=>※№→C++中 #include<>与#include""

    #include<> 使用尖括号表示在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找: #include"" 使用双引号则表示首先在 ...