zoukankan      html  css  js  c++  java
  • Highways prim() 没特别的意思

    Problem 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
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 using namespace std;
     7 int map[1001][1001];
     8 int cas,n,i,j;
     9 int prim()
    10 {
    11     int lowcost[1001],vis[1001];
    12     int maxn=-1;
    13     memset(vis,0,sizeof(vis));
    14     int it,jt,kt;
    15     for(int it=1;it<=n;it++)
    16     {
    17         lowcost[it]=map[1][it];
    18     }
    19     vis[1]=1;
    20     for(it=1;it<n;it++)
    21     {
    22         jt=1;
    23         while(vis[jt])jt++;
    24         for(kt=1;kt<=n;kt++)
    25         {
    26             if(!vis[kt]&&lowcost[kt]<lowcost[jt])jt=kt;
    27         }
    28         if(maxn<lowcost[jt])
    29             maxn=lowcost[jt];
    30         vis[jt]=1;
    31         for(kt=1;kt<=n;kt++)
    32         {
    33             if(!vis[kt]&&lowcost[kt]>map[kt][jt])
    34               lowcost[kt]=map[kt][jt];
    35         }
    36     }
    37     return maxn;
    38 }
    39 int main()
    40 {
    41     scanf("%d",&cas);
    42     while(cas--)
    43     {
    44         memset(map,0,sizeof(map));
    45         scanf("%d",&n);
    46         for(i=1;i<=n;i++)
    47          for(j=1;j<=n;j++)
    48            scanf("%d",&map[i][j]);
    49         printf("%d
    ",prim());
    50     }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    【51NOD 1478】括号序列的最长合法子段
    【BZOJ 3527】【ZJOI 2014】力
    【BZOJ 2194】快速傅立叶之二
    【CodeVS 3123】高精度练习之超大整数乘法 &【BZOJ 2197】FFT快速傅立叶
    【BZOJ 2693】jzptab
    【BZOJ 2154】Crash的数字表格
    【BZOJ 3529】【SDOI 2014】数表
    【BZOJ 2820】YY的GCD
    【BZOJ 2301】【HAOI 2011】Problem b
    【POJ 3294】Life Forms 不小于k个字符串中的最长子串
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3383198.html
Copyright © 2011-2022 走看看