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);
        }
    }
    

      

  • 相关阅读:
    CocoStudio基础教程(4)骨骼动画的动态换肤
    CocoStudio基础教程(3)在程序中处理cocoStudio导出动画
    CocoStudio基础教程(2)关联程序逻辑与cocoStudio导出文件
    CocoStudio基础教程(1)创建UI并载入到程序中
    LeetCode:盛最多水的容器【11】
    LeetCode:反转字符串中的元音字母【345】
    LeetCode:验证回文串【125】
    LeetCode:颜色分类【75】
    LeetCode:删除排序数组中的重复项||【80】
    LeetCode:移动零【283】
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5709301.html
Copyright © 2011-2022 走看看