• hdu 3371 Connect the Cities (最小生成树)


    Connect the Cities

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 8718    Accepted Submission(s): 2437


    Problem 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
     
    Author
    dandelion
     
    Source
     
    Recommend
    lcy   |   We have carefully selected several similar problems for you:  1198 1213 3372 3375 3376 
     
     
    分析时间复杂度:
       kruskal算法为O(e*lge),prim算法为O(v*v)(貌似可用优先队列缩减),此题数据为v=500,e=25000,理论上prim较优,但考虑到在kruskal算法find()处理过程中的路径压缩,使得两种算法耗时差不多。
     
     1 //546MS     1220K    1441B     C++    
     2 //G++ TLE,prim算法模板题 
     3 #include<stdio.h>
     4 #include<string.h>
     5 #define N 505
     6 #define INF 0x7ffffff
     7 int g[N][N];
     8 int low[N],vis[N];
     9 int n;
    10 int prim()
    11 {
    12     int pos,res=0;
    13     memset(vis,0,sizeof(vis));
    14     vis[1]=1;
    15     pos=1;
    16     for(int i=1;i<=n;i++)
    17         if(i!=pos) low[i]=g[pos][i];
    18     for(int i=1;i<n;i++){
    19         int minn=INF;
    20         pos=-1;
    21         for(int j=1;j<=n;j++)
    22             if(!vis[j] && minn>low[j]){
    23                 minn=low[j];
    24                 pos=j;
    25             }
    26         if(pos==-1) return -1; //注意这里 
    27         res+=minn;
    28         vis[pos]=1;
    29         //printf("**%d %d
    ",pos,minn);
    30         for(int j=1;j<=n;j++)
    31             if(!vis[j] && low[j]>g[pos][j])
    32                 low[j]=g[pos][j];
    33     }
    34     return res;
    35 }
    36 int main(void)
    37 {
    38     int t,m,k;
    39     int u,v,d;
    40     scanf("%d",&t);
    41     while(t--)
    42     {
    43         scanf("%d%d%d",&n,&m,&k);
    44         for(int i=0;i<=n;i++) 
    45             for(int j=0;j<=n;j++)
    46                 g[i][j]=INF;
    47         for(int i=0;i<m;i++){
    48             scanf("%d%d%d",&u,&v,&d);
    49             if(g[u][v]>d)    //notice 
    50                 g[u][v]=g[v][u]=d; 
    51         }
    52         for(int i=0;i<k;i++){
    53             scanf("%d",&d);
    54             scanf("%d",&u);
    55             d--;
    56             while(d--){
    57                 scanf("%d",&v);
    58                 g[u][v]=g[v][u]=0;
    59                 u=v;
    60             }
    61         }
    62         printf("%d
    ",prim());
    63     }
    64     return 0;
    65 }
  • 相关阅读:
    收音机原理
    S3C2440之存储控制器学习记录
    南校十二天集训游记
    题解 洛谷 P2179 【[NOI2012]骑行川藏】
    题解 SP3734 【PERIODNI
    题解 洛谷 P4177 【[CEOI2008]order】
    题解 CF613D 【Kingdom and its Cities】
    题解 UVA11865 【Stream My Contest】
    题解 洛谷 P2046 【[NOI2010]海拔】
    Spring Bean管理(注解的方式)
  • 原文地址:https://www.cnblogs.com/GO-NO-1/p/3710816.html
Copyright © 2020-2023  润新知