HYSBZ - 1050(旅行comf Java实现)

原题地址

解法:枚举每一条边,对于这条边,我们需要找到集合中和其值相差最小的最大边,这个集合是指与包括i边在内的ST联通集。对于这一要求,我们只需对所有的边进行从小到大的排序,那么从i边开始,一条条地加入并查集,一旦形成上述的联通集,立刻停止。

import java.io.*;
import java.util.*;
public class Main{
private static class edge{
int x,y,v;
}
private static class cmp implements Comparator<edge>{
@Override
public int compare(edge a,edge b){
return a.v<b.v?-1:1;
}
}
private static int fa[]=new int[505];
private static int Find(int x){
if(fa[x]==-1) return x;
return fa[x]=Find(fa[x]);
}
private static int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
private static final int N=5000+5;
private static edge e[]=new edge[N];
public static void main(String[] args){
int n,m,s,t;
Scanner sc=new Scanner(new InputStreamReader(System.in));
n=sc.nextInt();m=sc.nextInt();
for(int i=0;i<m;i++){
e[i]=new edge();
e[i].x=sc.nextInt();
e[i].y=sc.nextInt();
e[i].v=sc.nextInt();
}
s=sc.nextInt();
t=sc.nextInt();
Arrays.sort(e,0,m,new cmp());
int x1=0,y1=0;
for(int i=0;i<m;i++){
int ma=-1;
Arrays.fill(fa,-1);
for (int j=i; j<m; j++){
int f1=Find(e[j].x),f2=Find(e[j].y);
if (f1==f2) continue;
fa[f1]=f2;
if (Find(s)==Find(t)){
ma=e[j].v;
break;
}
}
if (ma==-1&&i==0){
System.out.printf("IMPOSSIBLE\n");
System.exit(0);
}
if (ma==-1) break;
if (x1==0||x1*e[i].v>ma*y1){
x1=ma;y1=e[i].v;
}
}
int x=gcd(x1,y1);
if (x==y1) System.out.printf("%d\n",x1/y1);
else System.out.printf("%d/%d\n",x1/x,y1/x);
sc.close();
}
}

HYSBZ - 1050(旅行comf 并查集Java实现)的更多相关文章

  1. BZOJ 1050 旅行comf 并查集+枚举下界

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1050 题目大意: 给你一个无向图,N(N<=500)个顶点, M(M<=5 ...

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

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

  3. bzoj 1050 [ HAOI 2006 ] 旅行comf —— 并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1050 没思路的话想想暴力就好了... 首先,比值最小就是确定最小值后最大值最小: 怎样确定最 ...

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

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

  5. BZOJ-1050 旅行comf 并查集+乱搞

    好久以前codevs上做过的,拿着改了改.. 1050: [HAOI2006]旅行comf Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2194 S ...

  6. 【BZOJ1050】[HAOI2006]旅行comf 并查集

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

  7. BZOJ1050 [HAOI2006]旅行comf[并查集判图连通性]

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

  8. BZOJ1050: [HAOI2006]旅行comf(并查集 最小生成树)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4021  Solved: 2257[Submit][Status][Discuss] Descript ...

  9. BZOJ 1050 旅行comf(枚举最小边-并查集)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1050 题意:给出一个带权图.求一条s到t的路径使得这条路径上最大最小边的比值最小? 思路 ...

随机推荐

  1. 3-1 todolist功能开发

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. WPF-CheckBox(复选框、功能开关)美化

    老规矩,先放图 按钮美化背景: 由于特殊需求,复选框样式单一,所以我们需要将其按钮重构和美化达到我们的需求 复选框美化思维引导: 图中1为背景色 图中2为边框 图中3为句柄控件组成(Path+Rect ...

  3. 基于ASP.Net Core开发一套通用后台框架记录-(总述)

    写在前面 本系列博客是本人在学习的过程中搭建学习的记录,如果对你有所帮助那再好不过.如果您有发现错误,请告知我,我会第一时间修改. 前期我不会公开源码,我想是一点点敲代码,不然复制.粘贴那就没意思了. ...

  4. 贪心 CodeForces 137B Permutation

    题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...

  5. 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)

    Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...

  6. 在Azure Ubunt Server 14.04虚机中使用Deep-Visualization-Toolbox

      参考网站 a)   https://zhuanlan.zhihu.com/p/24833574?utm_source=tuicool&utm_medium=referral b)   ht ...

  7. CentOS6.6从头到尾部署nginx与tomcat多实例

    前提条件: 1.需要一个全新的centos系统(本文中用到是centos6.6) 2.vmware虚拟机 3.vmware下安装centos系统,以NAT方式与宿主机相连 4.在centos系统中pi ...

  8. PSP辅助软件开发计划

    PSP辅助软件开发计划 作者: 日期:2013年11月14号 1开发目的 鉴于软件开发过程中,程序员往往无法在规定时间内完成任务,而且无法给出拖延的时间从而造成项目进度计划不准确.开发此软件帮助程序员 ...

  9. 【C++】智能指针简述(一):智能指针的引入

    智能指针是C++中一种利用RAII机制(后面解释),通过对象来管理指针的一种方式. 在C++中,动态开辟的内存需要我们自己去维护,在出函数作用域或程序异常退出之前,我们必须手动释放掉它,否则的话就会引 ...

  10. LockDemo 锁对象

    class Resource { private boolean flag = false; private String name; private int count; //资源锁 Lock lo ...