• HDU Today


    杭电2112

    View Code
     1 //杭电2112
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<stdlib.h>
     5 #define M 99999999
     6 #define N 160
     7 char s[N][50];
     8 int a[N],b[N],map[N][N];
     9 int n,t,num,k,p,d;
    10 int MIN(int a,int b)
    11 {
    12     return a<b?a:b;
    13 }
    14 int look(char a[])
    15 {
    16     int i;
    17     for(i=1;i<k;i++)
    18         if(strcmp(a,s[i])==0)
    19             break;
    20         if(i==k)
    21         {
    22             strcpy(s[k],a);
    23         k++;
    24         }
    25         return i;
    26 }
    27 void power()
    28 {
    29     char s1[50],s2[50];
    30     int i,j,x,y;
    31     scanf("%s%s",s1,s2);
    32     if(strcmp(s1,s2)==0)
    33         p=1;
    34     strcpy(s[1],s1);
    35     strcpy(s[2],s2);
    36     k=3;
    37     for(i=0;i<n;i++)
    38     {
    39         scanf("%s%s%d",s1,s2,&d);
    40         x=look(s1);
    41         y=look(s2);
    42         map[x][y]=map[y][x]=d;
    43     }
    44 }
    45 int main()
    46 {
    47     int i,j,t,min,num;
    48     while(scanf("%d",&n),n!=-1)
    49     {
    50         for(i=1;i<151;i++)
    51             for(j=1;j<151;j++)
    52             {
    53                 if(i==j)
    54                     map[i][j]=0;
    55                 else
    56                     map[i][j]=M;
    57             }
    58             p=0;
    59             power();
    60             if(p==1)
    61                 printf("0\n");
    62             else
    63             {
    64                 memset(b,0,sizeof(b));
    65                 for(i=1;i<k;i++)
    66                     a[i]=M;
    67                 t=j=1;
    68                 num=1;
    69                 b[1]=1;
    70                 a[1]=0;
    71                 while(num<k-1)
    72                 {
    73                     min=M;
    74                     for(i=2;i<k;i++)
    75                     {
    76                         if(b[i]==0&&map[t][i])
    77                         {
    78                             a[i]=MIN(a[i],a[t]+map[t][i]);//找出最短路径
    79                             if(min>a[i])
    80                             {
    81                                 min=a[i];
    82                                 j=i;
    83                             }
    84                         }
    85                     }
    86                     b[j]=1;
    
    87                     if(t==j||j==2)
    88                         break;
    89                     t=j;
    90                     num++;
    91                 }
    92                 if(a[2]!=M)
    93                     printf("%d\n",a[2]);
    94                 else
    95                     printf("-1\n");
    96             }
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    String
    Array常用方法
    Array类
    ruby调试/练习时的小技巧
    集合类对象的遍历处理办法
    Tech road one step 7-Noc to 13-Nov
    Tech road one step 31-Oct 6-Nov
    TechRoad_oneStep_17-23 10
    TechRoad_oneStep_1001
    TechRoad_oneStep_0919
  • 原文地址:https://www.cnblogs.com/zlyblog/p/2608594.html
Copyright © 2020-2023  润新知