zoukankan      html  css  js  c++  java
  • Ice_cream’s world III--2122

    Ice_cream’s world III

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 576    Accepted Submission(s): 185


    Problem Description
    ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
     

     

    Input
    Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
     

     

    Output
    If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
     

     

    Sample Input
    2 1
    0 1
    10
    4 0
     

     

    Sample Output
    10
    impossible
     
    思路:prime,考虑重边!!!
    AC代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 int map[1001][1001],dist[1001];
     4 int vis[1001],n;
     5 void init1()
     6 {
     7     int i,j;
     8     for(i = 0;i < n;i ++)
     9     {
    10         for(j = 0;j < n;j ++)
    11         {
    12             if(i != j)
    13                 map[i][j] = 1 << 30;
    14         }
    15     }
    16 }
    17 void init2()
    18 {
    19     int i;
    20     memset(vis,0,sizeof(vis));
    21     for(i = 0;i < n;i ++)
    22         dist[i] = map[0][i];
    23 }
    24 int main()
    25 {
    26     int m,i,j,k,cnt;
    27     int min,len,sum,a,b;
    28     while(~scanf("%d%d",&n,&m))
    29     {
    30         init1();
    31         sum = cnt = 0;
    32         while(m--)
    33         {
    34             scanf("%d%d%d",&a,&b,&len);
    35             if(len < map[a][b])
    36                 map[a][b] = map[b][a] = len;
    37         }
    38         init2();
    39         vis[0] = 1;
    40         for(i = 0;i < n;i ++)
    41         {
    42             min = 1 << 30;
    43             for(j = 0;j < n;j ++)
    44             {
    45                 if(!vis[j] && min > dist[j])
    46                 {
    47                     min = dist[j];
    48                     k = j;
    49                 }
    50             }
    51             vis[k] = 1;
    52             if(min != 1 << 30)
    53             {
    54                 cnt++;
    55                 sum += min;
    56             }
    57             for(j = 0;j < n;j ++)
    58             {
    59                 if(!vis[j] && dist[j] > map[k][j])
    60                     dist[j] = map[k][j];
    61             }
    62         }
    63         if(cnt == n-1)
    64             printf("%d
    
    ",sum);
    65         else
    66             printf("impossible
    
    ");
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    IOS开发之----协议与委托(Protocol and Delegate) 实例解析
    iOS开发-Protocol协议及委托代理(Delegate)传值
    android内置存储器memory和第三方外部存储disk管理
    余弦信号DFT频谱分析(继续)
    3.1存储管理操作系统
    android变化HOLO对话风格
    Coco2d-x android win7 Python 游戏开发环境的搭建
    ITIL该研究的结论(互联网思维的结合)
    亚马逊记AWS(Amazon Web Services)自由EC2应用
    oracle触发农产品证明文件号码
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3377254.html
Copyright © 2011-2022 走看看