Gym 100712F Travelling Salesman(二分+并查集)

/*********
Gym 100712F


Travelling Salesman




After leaving Yemen, Bahosain now works as a salesman in Jordan. He spends most of his time travelling
between different cities. He decided to buy a new car to help him in his job, but he has to decide about the
capacity of the fuel tank. The new car consumes one liter of fuel for each kilometer.
Each city has at least one gas station where Bahosain can refill the tank, but there are no stations on the roads
between cities.
Given the description of cities and the roads between them, find the minimum capacity for the fuel tank
needed so that Bahosain can travel between any pair of cities in at least one way.




Input
The first line of input contains T (1 ≤ T ≤ 64) that represents the number of test cases.
The first line of each test case contains two integers: N (3 ≤ N ≤ 100,000) and M (N-1 ≤ M ≤ 100,000), 
where N is the number of cities, and M is the number of roads.
Each of the following M lines contains three integers: X Y C (1 ≤ X, Y ≤ N)(X ≠ Y)(1 ≤ C ≤ 100,000), where
C is the length in kilometers between city X and city Y. Roads can be used in both ways.
It is guaranteed that each pair of cities is connected by at most one road, and one can travel between any pair
of cities using the given roads.




Output
For each test case, print a single line with the minimum needed capacity for the fuel tank.




Sample Input 
2
6 7
1 2 3
2 3 3
3 1 5
3 4 4
4 5 4
4 6 3
6 5 5


3 3
1 2 1
2 3 2
3 1 3




Sample Output
4
2




题目大意就是一个人要周游所有城市但他所驾驶的汽车的油箱容量有限,告诉你走这些城市之间的道路的所需要的油量,求可以使这个人可以到达所有城市的最小容量,也就是从任何一个城市可以到达另一个城市。
由于每条路的C值最大是100000,所以二分只需要不到20次。所以思路就是二分时用并查集判断整个图是否连通。这道题还有另一种思路,就是dijkstra算法,同样十分简单,不再赘述。


*********/


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=1e5+50;
int fa[MAXN],n,k;
struct node
{
    int a,b,v;
}road[MAXN];

int find(int n)
{
    if(fa[n]==n)
        return n;
    else
        return fa[n]=find(fa[n]);
}

void unite(int a,int b)
{
    int x=find(a);
    int y=find(b);
    fa[x]=y;
}

int judge(int m)
{
    for(int i=1;i<=n;i++)
    {
        fa[i]=i;
    }
    for(int i=0;i<k;i++)
    {
        if(road[i].v<=m)
        {
            unite(road[i].a,road[i].b);
        }
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(fa[i]==i)
            cnt++;
    }
    if(cnt==1)
        return 1;
    else
        return 0;
}

int main()
{
    //freopen("in.txt","r",stdin);
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        int l=1,r=1e5+50;
        scanf("%d%d",&n,&k);
        for(int i=0;i<k;i++)
        {
            scanf("%d%d%d",&road[i].a,&road[i].b,&road[i].v);
        }
        int cnt=20;
        while(cnt--)
        {
            int mid=(l+r)/2;
            if(judge(mid))//符合情况则向下二分
                r=mid;
            else
                l=mid+1;
        }
        printf("%d\n",l);
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PyCharm是一个集成开发环境(IDE),用于Python编程语言的开发。它提供了许多功能,例如代码编辑、调试和版本控制等。Gym是一个用于开发和比较强化学习算法的开源Python库。它提供了许多预定义的环境,供开发者进行强化学习实验。在安装深度强化学习环境的教程中,引用提到了在Windows 10上安装Anaconda、PyTorch、PyCharm和Gym等软件和包。引用提到了需要下载的软件和包,包括Anaconda、Pycharm、Python、PyTorch、Gym以及Pygame等。而引用中提到了通过pip命令来安装Gym库。综上所述,PyCharm和Gym都是用于Python开发和深度强化学习的工具和库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [DRL环境安装:Win10+Anaconda+PyTorch+PyCharm+Gym](https://blog.csdn.net/weixin_42480812/article/details/112054592)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [Anaconda+PyCharm+PyTorch+Gym深度强化学习环境搭建 送新手直接送进炼丹炉](https://blog.csdn.net/qq_43674552/article/details/127343863)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值