躲避拥堵的最佳路线

小明所在的城镇有m条路连接了n个区(n个区的编号在1~n的范围内),每条大道将两个区相连接,每条大道有一个拥挤度。小明想要开车从s区去t区,请你帮他规划一条路线,使得经过道路的拥挤度的最大值最小。
 

输入格式

第一行有四个用空格隔开的n,m,s,t,其含义见题目描述。

接下来m行,每行三个整数u,v,w,表示有一条大道连接区u和区v,且拥挤度为w。

两个区之间可能存在多条大道。

数据规模与约定
对于 30% 的数据,保证n≤10。
对于 60% 的数据,保证n≤100。
对于 100% 的数据,保证1≤n≤10^4,1≤m≤2×10^4, w≤10^4,1≤s,t≤n。且从 s 出发一定能到达 t 区。

输出格式

输出一行一个整数,代表最大的拥挤度。

样例
输入数据 1

    3 3 1 3
    1 2 2
    2 3 1
    1 3 3

输出数据 1

2

 

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Road{
    Road(int a,int b,int c):
        u(a),v(b),w(c)
    {}
    int u;
    int v;
    int w;
};

const int SIZE = 10005;
int parent[SIZE];

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

int main()
{
    //点的个数
    for(int i=0;i<SIZE;i++){
        parent[i] = i;
    }

    vector<Road> roads;
    int n,m,s,t;
    cin>>n>>m>>s>>t;
    int u,v,w;
    for(int i=0;i<m;i++){
        cin>>u>>v>>w;
        roads.emplace_back(u,v,w);
    }
    sort(roads.begin(),roads.end(),[](const Road& a,const Road& b){
        return a.w < b.w;
    });

    for(auto &road: roads){
        int a = find(road.u);
        int b = find(road.v);
        if(a!=b){
            parent[a] = b;
        }

        //s 和 t区第一次有通路了
        if(find(s) == find(t)){
            cout<<road.w<<endl;
            break;
        }
    }

    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值