【codevs 1001】舒适的路线

嘛嘛……
这题能做?

我滴妈……
按边排一遍序
然后恩恩
选择一条边当做最短的,然后往上找,之后找到的都会是比这条边大的,然后用并查集维护连通性,如果加入一条边之后可以从s走到t,就记录下答案,存下来
然后恩恩约分
233333

我不会约分……

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int MAXN = 500 + 5;
const int MAXM = 5000 + 5;
struct edge
{
    int f,t,v;
    bool operator < (const edge &b)const
    {
        return v < b.v;
    }
}l[MAXM];
int n,m;
int fa[MAXN];
int rank[MAXN];
void init()
{
    for(int i = 1;i <= n;i ++)
        fa[i] = i;
    memset(rank,0,sizeof(rank));
    return;
}
int find(int x)
{
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool same(int x,int y)
{
    return find(x) == find(y);
}
void merge(int x,int y)
{
    x = find(x);
    y = find(y);
    if(rank[x] < rank[y])
        swap(x,y);
    fa[x] = y;
    if(rank[x] == rank[y])
        rank[y] ++;
    return;
}
struct fs
{
    int fz,fm;
    bool operator < (const fs &b)const
    {
        return fz * b.fm < b.fz * fm;
    }
}zt[MAXM * MAXN];
int tot;
void solve(int s,int t)
{
    tot = 0;
    sort(l + 1,l + m + 1);
    for(int i = 1;i <= m;i ++)
    {
        init();
        zt[tot].fm = l[i].v;
        for(int j = i;j <= m;j ++)
        {
            merge(l[j].f,l[j].t);
            zt[tot].fz = l[j].v;
            if(same(s,t))
                break;
        }
        if(same(s,t))
            tot ++;
    }
    return;
}
int gcd(int a,int b)
{
    if(b == 0)
        return a;
    return gcd(b,a%b);
}
int s,t;
int main()
{
    scanf("%d %d",&n,&m);
    for(int i = 1;i <= m;i ++)
        scanf("%d %d %d",&l[i].f,&l[i].t,&l[i].v);
    scanf("%d %d",&s,&t);
    solve(s,t);
    sort(zt,zt + tot);
    if(!tot)
    {
        puts("IMPOSSIBLE");
        return 0;
    }
    if(zt[0].fz % zt[0].fm == 0)
    {
        printf("%d\n",zt[0].fz / zt[0].fm);
        return 0;
    }
    int k;
    while((k = gcd(zt[0].fm,zt[0].fz)) - 1)
    {
        zt[0].fm /= k;
        zt[0].fz /= k;
    }
    printf("%d/%d\n",zt[0].fz,zt[0].fm);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值