zoukankan      html  css  js  c++  java
  • ACM: 限时训练题解- 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

    Sample Output

    2

    4

    6

    7

    2

    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

    /*
    题意:
    旅游者想走遍全世界,一共有N个城市,他需要买一辆车,但是他抠,想买便宜点的就是油箱最小的
    每条路走过需要消耗 cost的油,找出最小的油箱需求。
    
    这题正好是前几天刷的最小生成树,排序后,维护最小树的最大边就行,代码就不多加注释了。 
    
    AC代码: 
    */ 
    
    #include"iostream"
    #include"algorithm"
    #include"cstdio"
    #include"cstring"
    #include"cmath"
    #define MX 100000 + 50
    using namespace std;
    
    int pe[MX];
    struct node {
    	int u,v,cost;
    } road[MX];
    
    bool cmp(node a,node b) {
    	return a.cost<b.cost;
    }
    
    int find(int x) {
    	return pe[x]==x?x:(pe[x]=find(pe[x]));
    }
    int main() {
    	int T,n,q,num,maxx;
    	scanf("%d",&T);
    	while(T--) {
    		scanf("%d%d",&n,&q);
    		for(int i=0; i<=n; i++) {
    			pe[i]=i;
    		}
    		num=n-1;
    		for(int i=0; i<q; i++) {
    			scanf("%d%d%d",&road[i].u,&road[i].v,&road[i].cost);
    		}
    		maxx=0;
    		sort(road,road+q,cmp);
    		for(int i=0; i<q; i++) {
    			int rt1=find(road[i].u);
    			int rt2=find(road[i].v);
    			if(rt1!=rt2) {
    				pe[rt2]=rt1;
    				maxx=max(maxx,road[i].cost);
    				num--;
    			}
    			if(!num)break;
    		}
    		printf("%d
    ",maxx);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    Android开发-httpclient兼容性问题
    Android开发-新建线程崩溃
    git 通过nginx 无法clone
    再谈git的http服务-权限控制gitweb版(未成功)
    再谈git的http服务-权限控制hooks版
    git push报错:error: RPC failed; result=22, HTTP code = 413
    再谈git的http服务
    Android开发-自动更新
    Android开发-Android Studio使用问题解决
    mac-文本编辑器
  • 原文地址:https://www.cnblogs.com/HDMaxfun/p/5709483.html
Copyright © 2011-2022 走看看