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

      

  • 相关阅读:
    单调栈
    【算法】验证码识别基础方法及源码
    获取安全时间
    用Cecil任意修改.Net程序集+源码
    【C#】纯托管实现一个Git服务端
    一个普通的但难以作答的面试题
    【WP7】欺骗你的地理坐标+源码
    【WP7】判断GPS坐标是否在中国
    完美的web 2.0站点用户中心标准,及开源用户中心nUserCenter进展报告
    开源 Asp.net mvc 用户中心开发计划
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5709301.html
Copyright © 2011-2022 走看看