• POJ2395 最小生成树


    题目:

    Out of Hay
    Time Limit: 1000MS        Memory Limit: 65536K
    Total Submissions: 10072        Accepted: 3885
    Description
    
    The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1. 
    
    Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry. 
    
    Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
    Input
    
    * Line 1: Two space-separated integers, N and M. 
    
    * Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
    Output
    
    * Line 1: A single integer that is the length of the longest road required to be traversed.
    Sample Input
    
    3 3
    1 2 23
    2 3 1000
    1 3 43
    Sample Output
    
    43
    Hint
    
    OUTPUT DETAILS: 
    
    In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.
    View Question

    以下代码来自《大话数据结构》,存在问题

     1 #include <stdio.h>
     2 #define MAXVEX 500
     3 #define INFINITY 65535
     4 
     5 typedef char VertexType;
     6 typedef int EdgeType;
     7 
     8 typedef struct {
     9     EdgeType arc[MAXVEX][MAXVEX];
    10     int numVertexes,numEdges;
    11 }MGraph;
    12 
    13 MGraph G;
    14 
    15 void CreateMGraph(MGraph *G){
    16     int i,j,k,w;
    17     scanf("%d %d",&G->numVertexes,&G->numEdges);//输入顶点和边数
    18     for(i = 0;i <= G->numVertexes; i++){
    19         for(j = 0;j <= G->numVertexes ;j++){
    20             G->arc[i][j]=INFINITY;
    21         }
    22     }
    23     for(k = 1;k<=G->numEdges;k++){
    24         scanf("%d %d %d",&i,&j,&w);
    25         G->arc[i][j]=w;
    26         G->arc[j][i]=G->arc[i][j];//因为是无向图,所以对称
    27     }
    28 }
    29 
    30 /* Prim算法生成最小生成树  */
    31 int MiniSpanTree_Prim(MGraph G){
    32     int min,i,j,k;
    33     int adjvex[MAXVEX];     /* 保存相关顶点下标 */
    34     int lowcost[MAXVEX];    /* 保存相关顶点间边的权值 */
    35     int dist_max;
    36     lowcost[1]=0;           /* 初始化第一个权值为0,即v0加入生成树 */
    37                             /* lowcost的值为0,在这里就是此下标的顶点已经加入生成树 */
    38     adjvex[1]=1;            /* 初始化第一个顶点下标为0 */
    39     dist_max=0;
    40     for(i=2;i<=G.numVertexes;i++){/* 循环除下标为0外的全部顶点 */
    41         lowcost[i]=G.arc[1][i];/* 将v0顶点与之有边的权值存入数组 */
    42         adjvex[i]=1;        /* 初始化都为v0的下标 */
    43     }
    44     for(i=2;i<=G.numVertexes;i++){
    45         min=INFINITY;       /* 初始化最小权值为∞, */
    46                             /* 通常设置为不可能的大数字如32767、65535等 */
    47         j=2;k=1;
    48         while(j<=G.numVertexes){/* 循环全部顶点 */
    49                 if(lowcost[j]!=0 && lowcost[j]<min){/* 如果权值不为0且权值小于min */
    50                     min = lowcost;/* 则让当前权值成为最小值 */
    51                     k=j;    /* 将当前最小值的下标存入k */
    52                 }
    53                 j++;
    54             }
    55         if(G.arc[1][k] > dist_max && G.arc[1][k] < INFINITY){
    56             dist_max=G.arc[1][k];
    57             //printf("%d
    ",)
    58         }
    59         //printf("(%d,%d)
    ",adjvex[k],k);  /* 打印当前顶点边中权值最小的边 */
    60 
    61         lowcost[k]=0;/* 将当前顶点的权值设置为0,表示此顶点已经完成任务 */
    62 
    63         for(j=2;j<=G.numVertexes;j++){/* 循环所有顶点 */
    64             if(lowcost[j]!=0 && G.arc[k][j]<lowcost[j]){
    65                 /* 如果下标为k顶点各边权值小于此前这些顶点未被加入生成树权值 */
    66                 lowcost[j]=G.arc[k][j];/* 将较小的权值存入lowcost相应位置 */
    67                 adjvex[j]=k;        /* 将下标为k的顶点存入adjvex */
    68             }
    69         }
    70     }
    71     return dist_max;
    72 }
    73 
    74 int main(){
    75     int max;
    76     CreateMGraph(&G);
    77     max=MiniSpanTree_Prim(G);
    78     printf("%d
    ",max);
    79     return 0;
    80 }

    AC代码来自冲神:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <cstring>
     6 #include <cmath>
     7 
     8 using namespace std;
     9 
    10 const int MAX = 2005;
    11 const int INF = 0x7fffffff;
    12 
    13 struct node
    14 {
    15     int to,nxt,len;
    16 } Map[20006];
    17 
    18 int n,m;
    19 int head[MAX],idx,dist[MAX];
    20 bool vist[MAX];
    21 
    22 void addNode(int cur,int to,int len)
    23 {
    24     Map[idx].to = to;
    25     Map[idx].nxt = head[cur];
    26     Map[idx].len = len;
    27     head[cur] = idx ++;
    28 }
    29 
    30 void prim(int cur)
    31 {
    32     while(cur)
    33     {
    34         for(int i = head[cur]; i != -1; i = Map[i].nxt)
    35         {
    36             int to = Map[i].to;
    37             if(vist[to] != true&&dist[to] > Map[i].len)
    38                 dist[to] = Map[i].len;
    39         }
    40         cur = 0;
    41         for(int i = 1; i <= n; i ++)
    42         {
    43             if(vist[i] == false && dist[i] < dist[cur])
    44             {
    45                 cur = i;
    46             }
    47         }
    48         vist[cur] = true;
    49     }
    50 }
    51 
    52 void init()
    53 {
    54     idx = 0;
    55     memset(head,-1,sizeof(head));
    56     fill(dist,dist+MAX,INF);
    57     memset(vist,0,sizeof(vist));
    58 }
    59 
    60 int main()
    61 {
    62     int a,b,c;
    63     while(scanf("%d%d",&n,&m)!=EOF)
    64     {
    65         init();
    66         for(int i = 1; i <= m; i ++)
    67         {
    68             scanf("%d%d%d",&a,&b,&c);
    69             addNode(a,b,c);
    70             addNode(b,a,c);
    71         }
    72         vist[1] = true;
    73         prim(1);
    74         int Max = 0;
    75         for(int i = 2; i <= n; i ++)
    76         {
    77             Max = max(Max,dist[i]);
    78             //printf("%d ",dist[i]);
    79         }
    80         printf("%d
    ",Max);
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    AspNet WebApi 中应用fo-dicom抛出异常:No codec registered for tranfer syntax:
    Codeforces Round #261 (Div. 2)459A. Pashmak and Garden(数学题)
    Android开发之使用Web Service进行网络编程
    线段树 + 扫描线加深具体解释
    对Shell几个冷知识的总结(IFS,数组,替换,分割,查找)
    lscript.ld 链接器脚本
    iOS 系统地图实现及定位
    Perl怎样过滤html标签
    hdu1213 How Many Tables(并查集)
    数据结构——链表
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/3581292.html
Copyright © 2020-2023  润新知