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

      

  • 相关阅读:
    使 Inno Setup 打包出的安装程序以管理员身份运行
    SQL SERVER 数据库被标记为“可疑”的解决办法
    SQL SERVER 查看数据库信息
    【转】如何正确地处理时间
    如何获取 XAML 控件的模板代码
    Asp.Net MVC 把PartialView、View转换成字符串
    表达式树在LINQ动态查询
    ASP.NET MVC5 实现分页查询
    WPF
    正则表达式定义
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5709301.html
Copyright © 2011-2022 走看看