• Ice_cream’s world III(prime)


    Ice_cream’s world III

    Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 6   Accepted Submission(s) : 3
    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
    题解:
    联通所有的路的最小代价;不连通就输出impossible
    prime代码:
     1 #include<stdio.h>
     2 #include<string.h>
     3 const int INF=0x3f3f3f3f;
     4 const int MAXN=1010;
     5 int N,M,answer;
     6 int map[MAXN][MAXN],low[MAXN],vis[MAXN];
     7 void prime(){
     8         int k,flot=1,temp;
     9         memset(vis,0,sizeof(vis));
    10         vis[0]=1;
    11         for(int i=0;i<N;i++)
    12             low[i]=map[0][i];
    13         for(int i=0;i<N;i++){
    14             temp=INF;
    15             for(int j=0;j<N;j++)
    16                 if(!vis[j]&&temp>low[j])temp=low[k=j];
    17             if(temp==INF){
    18                 if(flot==N)printf("%d
    ",answer);
    19                 else puts("impossible");
    20                 break;
    21             }
    22             answer+=temp;
    23             vis[k]=1;
    24             flot++;
    25             for(int j=0;j<N;j++)
    26                 if(!vis[j]&&low[j]>map[k][j])
    27                 low[j]=map[k][j];
    28         }
    29 }
    30 int main(){
    31     int a,b,c;
    32     while(~scanf("%d%d",&N,&M)){answer=0;
    33         memset(map,INF,sizeof(map));
    34         while(M--){
    35             scanf("%d%d%d",&a,&b,&c);
    36             if(c<map[a][b])
    37                 map[a][b]=map[b][a]=c;
    38         }
    39         prime();
    40         puts("");
    41     }
    42 return 0;
    43 }
  • 相关阅读:
    Eclipse中移除native层编译支持
    从Nginx源代码谈大写和小写字符转化的最高效代码以及ASCII码表的科学
    设计模式入门之原型模式Prototype
    Java 实现原型(Prototype)模式
    Eclipse
    图片3d轮放查看效果(V2.0):使用鼠标拖动实现图片的轮放
    Objective-C之成魔之路【0-序章】
    VC中常见API函数使用方法(经验版)
    zedboard中OLED源码
    Linux(C/C++)下的文件操作open、fopen与freopen
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4725313.html
Copyright © 2020-2023  润新知