[BZOJ1050][HAOI2006]旅行comf

29 篇文章 0 订阅

原题地址

水题.枚举最大边后用并查集维护连通性.

AC code:

#include <cstdio>
#include <algorithm>
using namespace std;
const int N=10010;
int n,m,S,T,a,b;
int f[N];

struct Edge{
    int u,v,w;

    friend bool operator<(Edge x,Edge y){
        return x.w<y.w;
    }
}E[N];

int find(int x){
    if(f[x]==x) return x;
    f[x]=find(f[x]);
    return f[x];
}

int gcd(int x,int y){
    return y?gcd(y,x%y):x;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++) scanf("%d%d%d",&E[i].u,&E[i].v,&E[i].w);
    scanf("%d%d",&S,&T);
    sort(E+1,E+m+1);
    for(int i=2;i<=m;i++){
        for(int j=1;j<=n;j++) f[j]=j;
        f[find(E[i].u)]=find(E[i].v);
        if(find(S)==find(T)){
            printf("1\n");
            return 0;
        }
        for(int j=i-1;j>=1;j--){
            f[find(E[j].u)]=find(E[j].v);
            if(find(S)==find(T)){
                if((!a)||(double)E[i].w/E[j].w<(double)a/b){
                    a=E[i].w;b=E[j].w;
                }
                break;
            }
        }
    }
    if(!a) printf("IMPOSSIBLE\n");
    else{
        int g=gcd(a,b);
        a/=g;b/=g;
        if(b==1) printf("%d\n",a);
        else printf("%d/%d\n",a,b);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值