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
  • 相关阅读:
    springboot项目引入通用mapper(tk.mybatis.mapper)的时候一些可能会踩的坑
    Zookeeper和Dubbo基本配置和调用
    Zookeeper和Dubbo介绍
    Git的基础命令大全,拉取推送,分支,合并,冲突
    Maven内置tomcat
    Maven高级,依赖传递和解决冲突,版本锁定,提取统一版本号
    SpringMvc的自定义异常,交给SpringMvc管理
    IDEA创建MavenWeb项目慢,问题根除
    Docker安装指南
    Docker容器入门
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3255506.html
Copyright © 2011-2022 走看看