zoukankan      html  css  js  c++  java
  • HDOJ 1874

    畅通工程续

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 22100    Accepted Submission(s): 7701

    Problem Description
    某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
    现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
     
    Input
    本题目包含多组数据,请处理到文件结束。 每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。 接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。 再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
     
    Output
    对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
     
    Sample Input
    3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
     
    Sample Output
    2 -1
     
    prim算法代码:
    9847261 2013-12-17 18:04:06 Accepted 1874 15MS 344K 997 B C++ 泽泽
     1 #include<stdio.h>
     2 #include<string.h>
     3 int g[201][201];
     4 #define inf 0xfffff
     5 void prim(int a,int n)
     6 {
     7     int used[201],lowcost[201],i,j,k,min;
     8     memset(used,0,sizeof(used));
     9     memset(lowcost,0,sizeof(lowcost));
    10     for(i=0;i<n;i++)
    11         lowcost[i]=g[i][a];
    12     used[a]=1;
    13     for(i=0;i<n;i++)
    14     {
    15         j=a;
    16         min=inf;
    17         for(k=0;k<n;k++)
    18             if(lowcost[k]<min&&!used[k])
    19                 min=lowcost[k],j=k;
    20         used[j]=1;
    21         for(k=0;k<n;k++)
    22         {
    23             if(lowcost[j]+g[k][j]<lowcost[k]&&!used[k])
    24                 lowcost[k]=lowcost[j]+g[k][j],
    25                 g[k][a]=g[a][k]=lowcost[k];
    26         }
    27     }
    28 
    29 
    30 }
    31 int main()
    32 {
    33     int n,m,i,j,a,b,x;
    34     while(scanf("%d %d",&n,&m)!=EOF)
    35     {
    36         for(i=0;i<n;i++)
    37             for(j=0;j<n;j++)
    38                 g[i][j]=inf;
    39         for(i=0;i<m;i++)
    40         {
    41             scanf("%d %d %d",&a,&b,&x);
    42             if(g[a][b]>x)
    43                 g[a][b]=g[b][a]=x;
    44         }
    45         int s,t;
    46         scanf("%d %d",&s,&t);
    47         if(s==t)
    48             printf("0
    ");
    49         else
    50         {
    51             prim(s,n);
    52         if(g[s][t]==inf)
    53             printf("-1
    ");
    54         else
    55           printf("%d
    ",g[s][t]);
    56         }
    57     }
    58     return 0;
    59 }
    View Code

     时间明显有优势!

    floyed算法代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 int g[201][201];
     4 #define inf 0xfffff
     5 
     6 void floyed(int n)
     7 {
     8     int i,j,k;
     9     for(k=0;k<n;k++)
    10     {
    11         for(i=0;i<n;i++)
    12         {
    13             for(j=0;j<n;j++)
    14             {
    15                 if(g[i][k]+g[k][j]<g[i][j])
    16                     g[i][j]=g[i][k]+g[k][j];
    17             }
    18         }
    19     }
    20 }
    21 int main()
    22 {
    23     int n,m,i,j,a,b,x;
    24     while(scanf("%d %d",&n,&m)!=EOF)
    25     {
    26         for(i=0;i<n;i++)
    27             for(j=0;j<n;j++)
    28                 g[i][j]=inf;
    29         for(i=0;i<m;i++)
    30         {
    31             scanf("%d %d %d",&a,&b,&x);
    32             if(g[a][b]>x)
    33                 g[a][b]=g[b][a]=x;
    34         }
    35         int s,t;
    36         scanf("%d %d",&s,&t);
    37         if(s==t)
    38             printf("0
    ");
    39         else
    40         {
    41             floyed(n);
    42         if(g[s][t]==inf)
    43             printf("-1
    ");
    44         else
    45           printf("%d
    ",g[s][t]);
    46         }
    47     }
    48     return 0;
    49 }
    View Code
    9847309 2013-12-17 18:12:17 Accepted 1874 31MS 340K 1196 B C++ 泽泽
  • 相关阅读:
    关于lockkeyword
    关于多层for循环迭代的效率优化问题
    Android 面试精华题目总结
    Linux基础回想(1)——Linux系统概述
    linux源代码编译安装OpenCV
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem B. Matrix Fast Power
    校赛热身 Problem B. Matrix Fast Power
    集合的划分(递推)
  • 原文地址:https://www.cnblogs.com/zeze/p/hdoj1874.html
Copyright © 2011-2022 走看看