UVA 10457 Magic Car——最小瓶颈路

其实就是瞎搞, 二重循环,第一层枚举最小值,第二层从最小值开始跑MST,当起点和终点在一个集合中时停止,此时求得的边权就是最大值,然后更新结果就好了

PS:第一次刷到vjudge时间第一

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 205;
const int maxm = 1005;
const int INF = 0x3f3f3f3f;

int n, m, scost, ecost, q, s, e, ans;
struct Edge {
    int from, to, cost;
    bool operator < (const Edge &temp) const {
        return cost < temp.cost;
    }
}edge[maxm];

int fa[maxn], ran[maxn];
void init(int x) {
    for (int i = 0; i <= x; i++) fa[i] = i, ran[i] = 0;
}
int query(int x) { return fa[x] == x ? x : fa[x] = query(fa[x]); }
inline void unite(int x, int y) {
    x = query(x), y = query(y);
    if (x == y) return;
    if (ran[x] < ran[y]) fa[x] = y;
    else {
        fa[y] = x;
        if (ran[x] == ran[y]) ran[x]++;
    }
}

int main() {
    while (~scanf("%d %d", &n, &m)) {
        for (int i = 1; i <= m; i++) scanf("%d %d %d", &edge[i].from, &edge[i].to, &edge[i].cost);
        sort(edge + 1, edge + 1 + m);
        scanf("%d %d %d", &scost, &ecost, &q);
        while (q--) {
            ans = INF;
            scanf("%d %d", &s, &e);
            for (int i = 1; i <= m; i++) {
                init(n);
                for (int j = i; j <= m; j++) {
                    if (edge[j].cost - edge[i].cost + scost + ecost >= ans) break;
                    unite(edge[j].from, edge[j].to);
                    if (query(s) == query(e)) { ans = min(ans, edge[j].cost - edge[i].cost + scost + ecost); break; }
                }
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值