zoukankan      html  css  js  c++  java
  • Highways poj 2485

    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 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
    
    3
    0 990 692
    990 0 179
    692 179 0

    Sample Output

    692
    

    题目大意:一个人当村长,想共享光纤,输入的是图,求最小生成树;这题和poj 1258 同一个代码,修改一行就可以

    思路: 按无向图处理;

             1.图转化成点到点的距离并储存起来

             2.按权有小到大排序

             3.Kruskal算法+并查集 构建最小生成树

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 struct node
     4 {
     5     int i,j;
     6     int weight;
     7 } s[150000];
     8 int cmp(const void*a,const void *b)
     9 {
    10     return (*(node*)a).weight-(*(node*)b).weight;
    11 }
    12 int v[505];
    13 int findl(int n)
    14 {
    15     return v[n]==n?n:findl(v[n]);
    16 }
    17 int main()
    18 {
    19     int m,n,i,j,k,a,sum;
    20     scanf("%d",&m);
    21     while(m--)
    22     {
    23         scanf("%d",&n);
    24         n++,k=0,sum=0;
    25         for(i=0; i<505; i++)
    26             v[i]=i;
    27         for(i=1; i<n; i++)
    28         {
    29             for(j=1; j<n; j++)
    30             {
    31                 scanf("%d",&a);
    32                 if(j>i)//0及以下的数字直接忽略
    33                 {
    34                     s[k].weight=a;
    35                     s[k].i=i,s[k].j=j,k++;
    36                 }
    37             }
    38         }
    39         qsort(s,k,sizeof(node),cmp);//按权值从小到大排序
    40         for(i=0; i<k; i++)
    41         {
    42             if(findl(s[i].i)!=findl(s[i].j))//简单的并查集
    43             {
    44                 if(sum<s[i].weight)
    45                     sum=s[i].weight;
    46                 v[findl(s[i].i)]=s[i].j;
    47             }
    48         }
    49         printf("%d
    ",sum);
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    CSS选择器的权重与优先规则
    excel上传--phpExcel读取xls、xlsx
    反射与代理设计模式
    Map集合
    接口实际应用-工厂代理模式
    代码模型:对象比较
    Stream数据流
    集合输出接口-Iterator迭代输出-古老枚举输出:Enumeration
    Set集合接口-HashSet_TreeSet理解
    List类集接口-ArrayList
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3255506.html
Copyright © 2011-2022 走看看