zoukankan      html  css  js  c++  java
  • hdu 1874 畅通工程续

    http://acm.hdu.edu.cn/showproblem.php?pid=1874

    畅通工程续

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


    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
    floyd
    View Code
     1 #include<stdio.h>
     2 #define INF 0x7ffffff
     3 int Graph[205][205];
     4 int town_num;
     5 int road_num;
     6 void floyd()
     7 {
     8     for(int i=0;i<town_num;i++)
     9     {
    10         for(int j=0;j<town_num;j++)
    11         {
    12             for(int k=0;k<town_num;k++)
    13             {
    14                 if(Graph[j][k]>Graph[j][i]+Graph[i][k])
    15                 Graph[j][k]=Graph[j][i]+Graph[i][k];
    16             }
    17         }
    18     }
    19 }
    20 int main()
    21 {
    22     int a,b,c;
    23     int min;
    24     int start,end;
    25     while(~scanf("%d%d",&town_num,&road_num))
    26     {
    27         for(int i=0;i<town_num;i++)
    28         {
    29             for(int j=0;j<town_num;j++)
    30             {
    31                 if(i==j) Graph[i][j]=0;
    32                 else Graph[i][j]=Graph[j][i]=INF;
    33             }
    34         }
    35         for(int i=0;i<road_num;i++)
    36         {
    37             scanf("%d%d%d",&a,&b,&c);
    38             if(Graph[a][b]>c)
    39             Graph[a][b]=Graph[b][a]=c;
    40         }
    41         scanf("%d%d",&start,&end);
    42         floyd();
    43         min=Graph[start][end];
    44         if(min==INF) printf("-1\n");
    45         else printf("%d\n",min);
    46     }
    47 }

    dijkstra

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define INF 0x7ffffff
     4 int town_num,road_num;
     5 int Graph[205][205];
     6 int dis[205];
     7 int vis[205];
     8 int s,e;
     9 void dijkstra()
    10 {
    11     int min;
    12     int now;
    13     memset(vis,0,sizeof(vis));
    14     for(int i=0;i<town_num;i++)
    15     {
    16         dis[i]=Graph[s][i];
    17     }
    18     vis[s]=1;
    19     dis[s]=0;
    20     for(int i=0;i<town_num;i++)
    21     {
    22         min=INF;
    23         for(int j=0;j<town_num;j++)
    24         {
    25             if(min>dis[j]&&!vis[j])
    26             {
    27                 min=dis[j];
    28                 now=j;
    29             }
    30         }
    31         vis[now]=1;
    32         if(min==INF) break;
    33         for(int j=0;j<town_num;j++)
    34         {
    35             if(!vis[j]&&dis[j]>min+Graph[now][j])
    36             dis[j]=min+Graph[now][j];
    37         }
    38         
    39     }
    40 
    41 }
    42 int main()
    43 {
    44     int a,b,c;
    45     int min;
    46     while(~scanf("%d%d",&town_num,&road_num))
    47     {
    48         for(int i=0;i<town_num;i++)
    49         for(int j=0;j<town_num;j++)
    50         Graph[i][j]=INF;
    51         for(int i=0;i<road_num;i++)
    52         {
    53             scanf("%d%d%d",&a,&b,&c);
    54             if(Graph[a][b]>c) 
    55             Graph[a][b]=Graph[b][a]=c;
    56         }
    57         scanf("%d%d",&s,&e);
    58         dijkstra();
    59         
    60         min=dis[e];
    61         if(min==INF) printf("-1\n");
    62         else printf("%d\n",min);
    63     }
    64 }
  • 相关阅读:
    分布式系统之CAP原理
    分布式缓存一致性哈希算法
    数据库三范式 无重复列 完全依赖主键 属性不依赖非主属性
    二叉树 B-树B+树
    数据库索引 主键 聚集索引 非聚集索引
    数据库水平拆分
    线程池ScheduledThreadPoolExecutor
    线程池之ThreadPoolExecutor
    mybatis一级缓存和二级缓存
    vue框架的搭建
  • 原文地址:https://www.cnblogs.com/1114250779boke/p/2640799.html
Copyright © 2011-2022 走看看