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 }
  • 相关阅读:
    c++ 连接mysql数据库
    c语言数据结构分析2之 链表插入删除
    c语言数据结构分析1之 链表创建
    微软 翻译工具
    c语言数据结构分析7之 二分查找
    sencha touch2 demo
    程序员装B指南
    c语言数据结构分析6之 快速排序
    c语言数据结构分析5之 冒泡
    对话jQuery之父John Resig:JavaScript的开发之路
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4725313.html
Copyright © 2011-2022 走看看