zoukankan      html  css  js  c++  java
  • 23.最短路径问题

    时间限制: 1 s

     空间限制: 32000 KB

     题目等级 : 黄金 Gold

    题解

     查看运行结果

    题目描述 Description

    平面上有n个点(n<=100),每个点的坐标均在-10000~10000之间。其中的一些点之间有连线。若有连线,则表示可从一个点到达另一个点,即两点间有通路,通路的距离为两点间的直线距离。现在的任务是找出从一点到另一点之间的最短路径。

    输入描述 Input Description

    第一行为整数n

    2行到第n+1行(共n行),每行两个整数xy,描述了一个点的坐标。

        n+2行为一个整数m,表示图中连线的个数。

        此后的m行,每行描述一条连线,由两个整数ij组成,表示第i个点和第j个点之间有连线。

        最后一行:两个整数st,分别表示源点和目标点。

    输出描述 Output Description

    仅一行,一个实数(保留两位小数),表示从st的最短路径长度。

    样例输入 Sample Input

    5

    0 0

    2 0

    2 2

    0 2

    3 1

    5

    1 2

    1 3

    1 4

    2 5

    3 5

    1 5

    样例输出 Sample Output

    3.41

    代码:

    #include

    using namespace std;

    #include

    #include

    #include

    #define maxn 101

    int n,x,y,m,s,t;

    int a[maxn][3];

    double p[maxn][maxn];

    double dis[maxn];

    bool exist[maxn];

    int team[10010],head,tail;//队列一定要足够大,否则用循环队列

    void input();

    void SPFA();

    int main()

    {

           input();

           SPFA();

           printf("%0.2lf",dis[t]);

           return 0;

    }

    void input()

    {

           scanf("%d",&n);

           for(int i=1;i<=n;++i)

           scanf("%d%d",&a[i][1],&a[i][2]);

           scanf("%d",&m);

           memset(p,99,sizeof(p));

           for(int i=1;i<=m;++i)

           {

                  scanf("%d%d",&x,&y);

                  p[x][y]=sqrt(pow(a[x][1]-a[y][1],2)+pow(a[x][2]-a[y][2],2));

                  p[y][x]=p[x][y];

           }

           scanf("%d%d",&s,&t);

           return;

    }

     

    void SPFA()

    {

           memset(dis,0x7f,sizeof(dis));

           memset(exist,false,sizeof(exist));

           team[1]=s;head=0;tail=1;

           exist[s]=true;

           dis[s]=0;

           do{

                  int u=team[++head];

                  exist[u]=false;

                  for(int i=1;i<=n;++i)

                  {

                         if(dis[i]>dis[u]+p[u][i])

                         {

                                dis[i]=dis[u]+p[u][i];

                                if(!exist[i])

                                {

                                       team[++tail]=i;

                                       exist[i]=true;

                                }

                         }

                  }

           }while(head<=tail);

    }

  • 相关阅读:
    最大子数组问题(分治策略实现)
    Solving the Detached Many-to-Many Problem with the Entity Framework
    Working With Entity Framework Detached Objects
    Attaching detached POCO to EF DbContext
    如何获取qq空间最近访问人列表
    Health Monitoring in ASP.NET 2.0
    problem with displaying the markers on Google maps
    WebMatrix Database.Open… Close() and Dispose()
    Accessing and Updating Data in ASP.NET: Retrieving XML Data with XmlDataSource Control
    Create web setup project that has crystal reports and sql script run manually on client system
  • 原文地址:https://www.cnblogs.com/c1299401227/p/5370800.html
Copyright © 2011-2022 走看看