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
  • 相关阅读:
    C/C++的64为长整型数的表示
    二分图带权匹配 KM算法与费用流模型建立
    常见的排序算法比较及总结
    个人联系方式
    (1) linux 3.x
    (4) linux 3.x
    (3) linux 3.x
    (2) linux 3.x
    (1) linux 3.x
    (3) linux 3.x
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3383198.html
Copyright © 2011-2022 走看看