zoukankan      html  css  js  c++  java
  • hdu 1874 畅通工程续 (Dijkstra or Floyd or Dfs)

    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

        Dijkstra:

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 #define mx 200000000;
     6 int map[205][205];
     7 int v[205],ans[205];
     8 int n;
     9 void f(int x)
    10 {
    11     v[x]=1;
    12     int i,p,mn;
    13     for (i=0;i<n;i++) ans[i]=map[x][i];
    14     while (1)
    15     {
    16         p=x;
    17         mn=mx
    18         for (i=0;i<n;i++)
    19         {
    20             if (!v[i]&&mn>ans[i])
    21             {
    22                 p=i;
    23                 mn=ans[i];
    24             }
    25         }
    26         if (p==x) return ;
    27         v[p]=1;
    28         for (i=0;i<n;i++)
    29         {
    30             if (!v[i]&&ans[i]>ans[p]+map[p][i])
    31                 ans[i]=ans[p]+map[p][i];
    32         }
    33     }
    34 }
    35 int main()
    36 {
    37     int m,i,j,a,b,c;
    38     while (~scanf("%d%d",&n,&m))
    39     {
    40         memset(v,0,sizeof(v));
    41         for (i=0;i<n;i++)
    42         for (j=0;j<n;j++)
    43         {
    44             if (i==j) map[i][j]=0;
    45             else map[i][j]=mx;
    46         }
    47         while (m--)
    48         {
    49             scanf("%d%d%d",&a,&b,&c);
    50             if (map[a][b]>c)
    51              map[a][b]=map[b][a]=c;
    52         }
    53         scanf("%d%d",&a,&b);
    54         f(a);
    55        if(ans[b]<200000000)
    56           printf("%d
    ",ans[b]);
    57        else printf("-1
    ");
    58     }
    59 }


        Floyd:

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 #define mx 200000000;
     6 int map[205][205];
     7 int n;
     8 void f()
     9 {
    10    int i,j,k;
    11    for (k=0;k<n;k++)
    12    for (i=0;i<n;i++)
    13    for (j=0;j<n;j++)
    14    if (map[i][j]>map[i][k]+map[k][j])
    15    map[i][j]=map[i][k]+map[k][j];
    16 }
    17 int main()
    18 {
    19     int m,i,j,a,b,c;
    20     while (~scanf("%d%d",&n,&m))
    21     {
    22         for (i=0;i<n;i++)
    23         for (j=0;j<n;j++)
    24         {
    25             if (i==j) map[i][j]=0;
    26             else map[i][j]=mx;
    27         }
    28         while (m--)
    29         {
    30             scanf("%d%d%d",&a,&b,&c);
    31             if (map[a][b]>c)
    32              map[a][b]=map[b][a]=c;
    33         }
    34        scanf("%d%d",&a,&b);
    35        f();
    36        if(map[a][b]<200000000)
    37           printf("%d
    ",map[a][b]);
    38        else printf("-1
    ");
    39     }
    40 }

      Dfs:

     1 #include<cstring>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 #define mx 200000000;
     6 int map[205][205];
     7 int v[205];
     8 int ans;
     9 int n;
    10 void dfs(int x,int y,int p)
    11 {
    12    if (p>ans) return ;
    13    if (x==y)
    14    {
    15        ans=p;
    16        return ;
    17    }
    18    int i;
    19    for (i=0;i<n;i++)
    20    {
    21        if (v[i]!=-1&&map[x][i])
    22        {
    23            if (v[i]&&v[i]<=p+map[x][i]) continue;
    24            v[i]=p+map[x][i];
    25            dfs(i,y,p+map[x][i]);
    26        }
    27    }
    28 }
    29 int main()
    30 {
    31     int m,i,j,a,b,c;
    32     while (~scanf("%d%d",&n,&m))
    33     {
    34         memset(v,0,sizeof(v));
    35         for (i=0;i<n;i++)
    36         for (j=0;j<n;j++) map[i][j]=0;
    37         while (m--)
    38         {
    39             scanf("%d%d%d",&a,&b,&c);
    40             if (map[a][b]>c||map[a][b]==0)
    41              map[a][b]=map[b][a]=c;
    42         }
    43        scanf("%d%d",&a,&b);
    44        ans=mx;
    45        v[a]=-1;
    46        dfs(a,b,0);
    47        if(ans<200000000)
    48           printf("%d
    ",ans);
    49        else printf("-1
    ");
    50     }
    51 }
  • 相关阅读:
    【串线篇】Mybatis缓存原理
    【串线篇】Mybatis之动态sql
    【串线篇】Mybatis之模糊查询
    【串线篇】sql映射文件-分布查询(上)association 1-1
    【串线篇】SQL映射文件-联合查询(完结association+cellection)
    【串线篇】加谈数据库之连接join
    【串线篇】数据库设计之加谈n-n
    【串线篇】SQL映射文件-resultMap自定义封装
    【串线篇】sql注入问题
    【串线篇】SQL映射文件select简单查询标签
  • 原文地址:https://www.cnblogs.com/pblr/p/4748902.html
Copyright © 2011-2022 走看看