B - Highways

题目来源:POJ - 2485 

 

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 of input is an integer T, which tells how many test cases followed. 
The first line of each case 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. There is an empty line after each test case. 
Output 
For each test case, 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. 
Sample Input 
1


0 990 692 
990 0 179 
692 179 0 
Sample Output 
692 
Hint 
Huge input,scanf is recommended.


题目大意就是:用矩阵给出各点之间的距离。采用最短的方式使所有的点连通。输出最佳方式中所需要修建的最长路的距离。


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAX 999999
using namespace std;
int pre[100001];
int n,m;
struct node
{
    int u,v;
    int value;//存储距离
}p[300000];
//这四个函数还是和并查集的一样。
void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        pre[i]=i;
    }
}//根据初始化pre[]数组

int find(int x)
{
    if(pre[x]==x) return x;
    return pre[x]=find(pre[x]);
}//找到根节点

bool cmp (node x,node y)
{
    return x.value<y.value;
}

void mix(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        pre[fy]=fx;
    }
}//合并集合。将根节点不同的两个村落连接起来。

int main()
{
    int t,s;
    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        init(n);
        int k=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&s);
                p[k].u =i;
                p[k].v =j;//此时的I J 代表了两个村落。
                p[k++].value =s;//s代表IJ两个村落间的距离
            }
        }
        sort(p,p+k,cmp);//将所有距离排序,
        int dis=0;
        for(int i=0;i<k;i++)
        {
            if(find(p[i].u)!=find(p[i].v))//如果两个村落的根节点不同
            {

                mix(p[i].u ,p[i].v );//将其连接起来。
                if(p[i].value>dis)
                    dis=p[i].value ;//用于统计最长的那条路。
            }
        }
        printf("%d\n",dis);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sklearn中自带了波士顿房价数据集,可以通过以下代码导入: ``` from sklearn.datasets import load_boston boston = load_boston() X = boston.data # 特征矩阵 y = boston.target # 目标向量 ``` 其中,X是一个13维的特征矩阵,y是一个样本数量为506的目标向量。可以通过以下代码查看数据集的描述: ``` print(boston.DESCR) ``` 输出结果如下: ``` .. _boston_dataset: Boston house prices dataset --------------------------- **Data Set Characteristics:** :Number of Instances: 506 :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target. :Attribute Information (in order): - CRIM per capita crime rate by town - ZN proportion of residential land zoned for lots over 25,000 sq.ft. - INDUS proportion of non-retail business acres per town - CHAS Charles River dummy variable (= 1 if tract bounds river; 0 otherwise) - NOX nitric oxides concentration (parts per 10 million) - RM average number of rooms per dwelling - AGE proportion of owner-occupied units built prior to 1940 - DIS weighted distances to five Boston employment centres - RAD index of accessibility to radial highways - TAX full-value property-tax rate per $10,000 - PTRATIO pupil-teacher ratio by town - B 1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town - LSTAT % lower status of the population - MEDV Median value of owner-occupied homes in $1000's :Missing Attribute Values: None :Creator: Harrison, D. and Rubinfeld, D.L. This is a copy of UCI ML housing dataset. https://archive.ics.uci.edu/ml/machine-learning-databases/housing/ This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University. The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic prices and the demand for clean air', J. Environ. Economics & Management, vol.5, 81-102, 1978. Used in Belsley, Kuh & Welsch, 'Regression diagnostics ...', Wiley, 1980. N.B. Various transformations are used in the table on pages 244-261 of the latter. The Boston house-price data has been used in many machine learning papers that address regression problems. **References** - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261. - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值