zoukankan      html  css  js  c++  java
  • Codeforces Travelling Salesman 最小生成树

    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

    题意:城市里才有加油站,每一个城市都要去求油箱最小容量

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    
    int t,n,m;
    int father[100005];
    
    struct node
    {
        int st,ed,cos;
    }a[100005];
    
    int cmp(node a,node b)
    {
        return a.cos<b.cos;
    }
    
    void Init(int x)
    {
        for(int i=1;i<=x;i++)
            {
                father[i]=i;
            }
    }
    
    int Find(int x)
    {
        if(x!=father[x])
        {
            father[x]=Find(father[x]);
        }
        return father[x];
    }
    
    int Union(int x,int y)
    {
        x=Find(x);
        y=Find(y);
                //cout<<x<<"   fff   "<<y<<endl;
    
        if(x==y)
            return 0;
        father[x]=y;
        return 1;
    }
    
    int main()
    {
        //freopen("input.txt","r",stdin);
        scanf("%d",&t);
        while(t--)
        {
              int res=0;
              scanf("%d%d",&n,&m);
              Init(n);
              for(int i=1;i<=m;i++)
                scanf("%d%d%d",&a[i].st,&a[i].ed,&a[i].cos);
    
              sort(a+1,a+1+m,cmp);
              for(int i=1;i<=m;i++)
              {
                      if(Union(father[a[i].st],father[a[i].ed]))
                      res=max(res,a[i].cos);
              }
              printf("%d
    ",res);
        }
    }
    

      

  • 相关阅读:
    Pycharm 调试system-config-users
    只写了两行代码,为什么要花两天时间?
    为开源做贡献的6个技巧
    2020年10月编程语言排行榜
    全球最厉害的 14 位程序员
    6_38_二叉树的后序遍历非递归算法(和先序有些许不一样)
    6_37_二叉树的先序遍历非递归算法
    6_36_相似二叉树
    6_33_两个一维数组判断u是否为v的子孙
    6_34_扩展判断u是否为v的子孙
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5709301.html
Copyright © 2011-2022 走看看