http://www.lydsy.com/JudgeOnline/problem.php?id=1050

表示被暴力吓到了orz

我竟然想不到。。。我竟然还想到分数规划,,但是不可做。。。然后又想到最小生成树,,然后不会做orz

我一直在纠结怎么最大化(或最小化)分母和最小化(或最大化)分子的做法。。。。。但是。。。。不会orz

没想到是暴力orz

直接排序后枚举最小的边,生成树后要最大的边最小(排序后即可orz),然后更新答案即可。

但是不知道之前写错了啥wa了两发,,,随便改改才过orz

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mkpii make_pair<int, int>
#define pdi pair<double, int>
#define mkpdi make_pair<double, int>
#define pli pair<ll, int>
#define mkpli make_pair<ll, int>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define error(x) (!(x)?puts("error"):0)
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=1005, M=10005;
struct dat { int x, y, w; }a[M];
inline const bool cmp(const dat &a, const dat &b) { return a.w<b.w; }
int p[N], ans1, ans2, n, m;
const int ifind(const int &x) { return x==p[x]?x:p[x]=ifind(p[x]); }
const int gcd(const int &a, const int &b) { return !b?a:gcd(b, a%b); } int main() {
read(n); read(m);
for1(i, 1, m) read(a[i].x), read(a[i].y), read(a[i].w);
int s=getint(), t=getint();
sort(a+1, a+1+m, cmp);
bool flag=1;
for1(i, 1, m) {
for1(j, 1, n) p[j]=j;
int j=i;
for(; j<=m; ++j) {
int fx=ifind(a[j].x), fy=ifind(a[j].y);
p[fx]=fy;
if(ifind(s)==ifind(t)) {
flag=0;
if(a[j].w*ans2<=a[i].w*ans1) ans1=a[j].w, ans2=a[i].w;
break;
}
}
if(ifind(s)!=ifind(t)) break;
if(a[j].w*ans2<=a[i].w*ans1) ans1=a[j].w, ans2=a[i].w;
}
if(flag) { puts("IMPOSSIBLE"); return 0; }
int d=gcd(ans1, ans2);
ans1/=d; ans2/=d;
if(ans2==1) printf("%d\n", ans1);
else printf("%d/%d\n", ans1, ans2);
return 0;
}

  


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

 

【BZOJ】1050: [HAOI2006]旅行comf(暴力+并查集)的更多相关文章

  1. bzoj 1050 [HAOI2006]旅行comf (并查集)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1050 思路: 先将每条边的权值排个序优先小的,然后从小到大枚举每一条边,将其存到并查集 ...

  2. BZOJ 1050: [HAOI2006]旅行comf( 并查集 )

    将edge按权值排序 , O( m² ) 枚举边 , 利用并查集维护连通信息. ------------------------------------------------------------ ...

  3. BZOJ 1050 [HAOI2006]旅行comf

    1050: [HAOI2006]旅行comf Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1889  Solved: 976[Submit][Sta ...

  4. bzoj 1050: [HAOI2006]旅行comf(codevs.cn 1001 舒适的路线) 快排+并查集乱搞

    没用的话:好像很久没发博客了,主要是懒太蒟找不到水题.我绝对没弃坑...^_^ 还用些话:本文为博主原创文章,若转载请注明原网址和作者. 进入正题: 先pa网址: bzoj :http://www.l ...

  5. BZOJ 1050: [HAOI2006]旅行comf (并查集 或 单调队列)

    这是建空间后做的第一道题啊= =好水 排序,枚举最小边,然后并查集求出联通时的最大边 或者排次序,从小到大插边,如果插边时最小的边拿掉不会使s与t不联通,就删去。 code: #include< ...

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

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

  7. bzoj 1050: [HAOI2006]旅行comf&&【codevs1001】

    Description 给你一个无向图,N(N<=500)个顶点, M(M<=5000)条边,每条边有一个权值Vi(Vi<30000).给你两个顶点S和T,求 一条路径,使得路径上最 ...

  8. bzoj 1050 [HAOI2006]旅行comf——kruscal

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1050 因为还有Impossible的情况,所以想到了kruscal.(?) 但好像不太行.然 ...

  9. BZOJ 1050 [HAOI2006]旅行comf(最小生成树)

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

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

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

随机推荐

  1. 转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes)

    转换到 StoryBoard 的公布说明(Converting to Storyboards Release Notes) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl ...

  2. ionic 调用手机的打电话功能

    1.需求描述 在ionic项目用调用手机的打电话功能.开始还想找cordova和ng-cordova的插件那,现在H5实现起来特别方便. 2.准备 在cordova中所有的URL Schemes 都是 ...

  3. JavaScript indexof方法

    1.indexof方法 indexof方法可以在字符串和数组上使用. 2.字符串使用 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置. <!DOCTYPE html&g ...

  4. Matlab interpgui

    function interpgui(arg1,arg2) %INTERPGUI Behavior of interpolating functions. % Demonstrates interpo ...

  5. windows在与time.windows.com进行同步时出错

      windows在与time.windows.com进行同步时出错 CreateTime--2017年6月29日10:28:16Author:Marydon 参考地址:http://www.jb51 ...

  6. 【Django】ImportError: cannot import name 'execute_manager'

    错误描述 在使用Django 2.0 启动项目的时候,报错如下: ImportError: cannot import name 'execute_manager' 修改前后代码对比 修改前的代码 # ...

  7. web站点,同一个浏览器只能登陆一个用户的原因(cookie不能跨浏览器)

    我的web站点,比如  http://ip/testsite/default.aspx, 当我在我的机器上,用chrome打开,用账号user1登陆,那么当我再新开个tab,再打开这个web站点,这时 ...

  8. Linux-软件包管理-RPM安装位置\源码包安装位置

    rpm -ql httpd 查看apache包中文件的安装位置 find /etc -name httpd 查找apache程序的启动执行httpd所在位置 cd /etc/rc.d/init.d 切 ...

  9. EMQ学习---客户链接资源消耗

    Erlang进程消耗EMQ对客户端链接使用链接进程(emqtt_client)和session进程(emqtt_session)分开的策略. 当一个mqtt的客户端连接到EMQ的服务器上的时候,首先会 ...

  10. MySQL主从复制状态及数据一致性监测工具

    转: https://blog.csdn.net/m0_37814112/article/details/80914713