zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    DVB数字电视常见信号指标解释
    傅里叶变换与不确定性
    Sigrity PowerDC是如何计算IR Drop Margin?
    带你了解强大的Cadence家族,你可能只用到了它1/10的工具
    在线原理图绘制网站推荐
    FinFET简介
    SPICE简史
    时域反射计(TDR)原理与应用
    PCB仿真软件与电磁场求解器的算法
    Jersey 2 + Maven + Tomcat + IntelliJ IDEA 搭建RESTful服务
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4725313.html
Copyright © 2011-2022 走看看