zoukankan      html  css  js  c++  java
  • Connect the Cities(MST prim)

    Connect the Cities
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    In 2100, since the sea level rise, most of the cities disappear. Though some survived cities are still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don’t want to take too much money.  
     

    Input

    The first line contains the number of test cases.  Each test case starts with three integers: n, m and k. n (3 <= n <=500) stands for the number of survived cities, m (0 <= m <= 25000) stands for the number of roads you can choose to connect the cities and k (0 <= k <= 100) stands for the number of still connected cities. To make it easy, the cities are signed from 1 to n.  Then follow m lines, each contains three integers p, q and c (0 <= c <= 1000), means it takes c to connect p and q.  Then follow k lines, each line starts with an integer t (2 <= t <= n) stands for the number of this connected cities. Then t integers follow stands for the id of these cities. 
     

    Output

    For each case, output the least money you need to take, if it’s impossible, just output -1.
     

    Sample Input

    1 6 4 3 1 4 2 2 6 1 2 3 5 3 4 33 2 1 2 2 1 3 3 4 5 6
     

    Sample Output

    1
     
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 using namespace std;
     4 #define N 505
     5 #define INF 0x7fffffff
     6 int n , m , k ;
     7 int g[N][N] , vis[N] , low[N] ;
     8 
     9 void prim ()
    10 {
    11     int pos = 1 , res = 0 ;
    12     memset (vis , 0 , sizeof(vis)) ;
    13     vis[pos] = 1 ;
    14     for (int i = 1 ; i <= n ; i++)
    15         if ( !vis[i] )
    16             low[i] = g[pos][i] ;
    17     for (int i = 1 ; i < n ; i++) {
    18         int minn = INF ;
    19         pos = -1 ;
    20         for (int j = 1 ; j <= n ; j++)
    21             if ( !vis[j] && low[j] < minn) {
    22                 minn = low[j] ;
    23                 pos = j ;
    24         }
    25         if (pos == -1) {
    26             puts ("-1") ;
    27             return ;
    28         }
    29         res += minn ;
    30         vis[pos] = 1 ;
    31         for (int j = 1 ; j <= n ; j++)
    32             if (!vis[j] && low[j] > g[pos][j])
    33                 low[j] = g[pos][j] ;
    34     }
    35     printf ("%d
    " , res) ;
    36 }
    37 int main()
    38 {
    39    // freopen ("a.txt" , "r" , stdin);
    40     int u , v , w ;
    41     int T , t;
    42     scanf ("%d" , &T) ;
    43     while (T--) {
    44         scanf ("%d%d%d" , &n , &m , &k) ;
    45         for (int i = 0 ; i <= n ; i++)
    46             for (int j = 0 ; j <= n ; j++)
    47                 g[i][j] = INF ;
    48         for (int i = 0 ; i < m ; i++) {
    49             scanf ("%d%d%d" , &u , &v , &w) ;
    50             if (g[u][v] > w)//notice
    51                 g[u][v] = g[v][u] = w ;
    52         }
    53         for (int i = 0 ; i < k ; i++) {
    54             scanf ("%d%d" , &t , &u) ;
    55             for (int j =1 ; j <= t - 1 ; j++) {
    56                 scanf ("%d" , &v) ;
    57                 g[u][v] = g[v][u] = 0 ;
    58             }
    59         }
    60         prim () ;
    61     }
    62 }

    给偶上了一课,本以为用kruskal就能一条路走到黑:

    但遗憾的TLE了,

    分析时间复杂度:
       kruskal算法为O(e*lge),prim算法为O(v*v)(貌似可用优先队列缩减),此题数据为v=500,e=25000,用prim较优
    转载:http://www.cnblogs.com/GO-NO-1/p/3710816.html
     
  • 相关阅读:
    Mac保留Python2安装Python3(Anaconda3)
    Mybatis Plugin 以及Druid Filer 改写SQL
    Jackson替换fastjson
    Java单元测试 Http Server Mock框架选型
    应用中有多个Spring Property PlaceHolder导致@Value只能获取到默认值
    RabbitMQ Policy的使用
    Collectors.toMap不允许Null Value导致NPE
    php 删除标签
    php 替换标签
    jquery拼接html
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4290188.html
Copyright © 2011-2022 走看看