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 }
  • 相关阅读:
    梯度下降(一)
    springcloud中的gateway中的配置访问方式
    hyper-v powershell 开通虚拟机,配置vlan,配置CPU,内存
    DOCKER 安装zabbix_proxy进行代理监控
    LINUX lvm扩容
    ipset 如何在 Linux 下大量屏蔽恶意 IP 地址
    变量、常量和注释
    pycharm使用和虚拟环境
    编程语言和python介绍及安装
    Spring详解(三)——认识IoC控制反转/DI依赖注入
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4725313.html
Copyright © 2011-2022 走看看