zoukankan      html  css  js  c++  java
  • soj1090.Highways

    1090. Highways

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

    Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

    The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

    Input

    The first line is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. 

    Output

    You should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.
    This problem contains multiple test cases!
    The first line of a multiple input is an integer T, then a blank line followed by T input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
    The output format consists of T output blocks. There is a blank line between output blocks.

    Sample Input

    1
    
    3
    0 990 692
    990 0 179
    692 179 0
    

    Sample Output

    692

    使用kruskal算法,注意并查集技术

    #include <iostream>
    #include <set>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int dis[501][501];
    struct Edge
    {
    	int a;
    	int b;
    	int w;
    	Edge(int aa,int bb,int ww)
    	{
    		a = aa;
    		b = bb;
    		w = ww;
    	}
    };
    
    bool cmp(Edge aa,Edge bb)
    {
    	return aa.w < bb.w;
    }
    
    int a[501];
    
    //使用并查集实现kruskal算法
    int find(int x)
    {
    	if(x == a[x])
    		return x;
    	else
    	{
    		a[x] = find(a[x]);
    		return a[x];
    	}
    }
    
    
    int main()
    {
    	int T;
    	cin >> T;
    	while(T--)
    	{
    		set<int> ss;
    		vector<Edge> tt;
    		int N;
    		cin >> N;
    		int i,j;
    		for(i = 1;i <= N;i++)
    		{
    			for(j = 1;j <= N;j++)
    			{
    				cin >> dis[i][j];
    				if(i > j)
    					tt.push_back(Edge(i,j,dis[i][j]));
    			}
    		}
    		sort(tt.begin(),tt.end(),cmp);
    		int max = 0;
    		int num = 0;
    		for(i = 1;i <= N;i++)
    			a[i] = i;
    		for(i = 0;i < tt.size();i++)
    		{
    			if(find(tt[i].a) != find(tt[i].b))
    			{
    				num++;
    				max = tt[i].w;
    				a[find(tt[i].a)] = a[find(tt[i].b)];
    			}
    			if(num == N-1)
    				break;
    		}
    		cout << max << endl;
    		if(T != 0)
    			cout << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    .NET Obfuscator Dotfuscator 入门
    查询集合已修改;可能无法执行枚举操作
    泛型(C# 2。0 编程指南) <一>
    在服务器上部署VS 2008 ReportViewer,完美支持中文
    dataGridView 闪烁 和 listview 闪烁 的解决办法。
    Asp.Net 调试客户端脚本
    疑是Microsoft Enterprise Library June 2005的一个小bug (续)
    MagicAjax 0.30版的更新(翻译)
    疑是Microsoft Enterprise Library June 2005的一个小bug
    在web页面中水晶报表显示速度过慢的原因
  • 原文地址:https://www.cnblogs.com/sysu-blackbear/p/3278540.html
Copyright © 2011-2022 走看看