The shortest path (through an information NOI 1342)

Description [title]

There are n points (n≤100) plane, the coordinates of each point are between -10,000 to 10,000. There are a number of connection points between them.

If the connection, then can be reached from one point to another point, that path between two points, distance of a straight line path distance between two points. The task now is to find the shortest path from one point to another.

[Enter]

Total n + m + 3 lines, wherein:

The first line integer n.

Row 2 to row n + 1 (of n lines), each row two integers x and y, coordinates of a point are described.

A behavior of the integer n + 2 m, the figure represents the number of connection.

After the m rows, each row describes a connection of two integers i and j consisting, expressed the connection point between the i-th and j-th points.

Last line: two integers s and t, respectively, represent the source and destination points.

[Output]

Line, a real number (two decimal places), represents the length of the shortest path from s to t.

[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


1. Floyed-Warshall algorithm is O (N3) n <= 500

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[101][3], n,i,j,k,x,y,m,s,e;
 4 double f[101][101];
 5 int main()
 6 {
 7     cin >> n;
 8     for (i = 1; i <= n; i++)
 9         cin >> a[i][1] >> a[i][2];                //第i个点的坐标
10     cin >> m;
11     Memset (f, 0x3F , the sizeof (f));                  // initialize the array to a maximum value f 
12 is     for (I = . 1 ; I <= m; I ++)                             // pretreatment of the x, the distance between Y 
13 is      {
 14        CIN >> Y >> X;     // POW (x, y) represents the x ^ y, wherein x, y must be double type, use cmath library 
15        F [Y] [X] = F [X] [Y] = sqrt ( POW ( Double (A [X] [ . 1 ] -a [Y] [ . 1 ]), 2 ) + POW ( Double (A [X] [ 2 ] -a [Y] [ 2 ]), 2 ));     
 16      }
 17     cin >> s >> e;
18     for (k = 1; k <= n; k++)                     //floyed 最短路算法
19        for (i = 1; i <= n; i++)
20           for (j = 1; j <= n; j++)
21              if ((i != j) && (i != k) && (j != k) && (f[i][k]+f[k][j] < f[i][j]))
22                  f[i][j] = f[i][k] + f[k][j];
23     printf("%.2lf\n",f[s][e]);
24     return 0;  
25 }

 2. the Dijkstra algorithm is O (N2) non-optimized n <= 5000 greedy

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[101][3] ,f[101][101];;
 4 double c[101];
 5 bool b[101];
 6 int n,i,j,k,x,y,m,s,e;
 7 double minl;
 8 double maxx = 1e30;
 9 int main()
10 {
11     cin >> n;
12     for (i = 1; I <= n-; I ++ )
 13 is        CIN >> A [I] [ . 1 ] >> A [I] [ 2 ];
 14      for (I = . 1 ; I <= n-; I ++ )
 15        for (J = . 1 ; J <= n-; J ++ )
 16          F [I] [J] = Maxx;                          // F maximum array initialization 
. 17      CIN >> m;
 18 is      for (I = . 1 ; I <= m; I ++)                     // pretreatment xy the distance between F [X] [Y] 
. 19      {
 20 is          CIN >> >> X Y;
 21 is          F [X] [Y] = F [Y] [X] = sqrt (POW (double(a[x][1]-a[y][1]),2)+pow(double(a[x][2]-a[y][2]),2));
22     }
23     cin >> s >> e;
24     for (i = 1; i <= n; i++) 
25       c[i] = f[s][i];                            //初始化
26     memset(b,false,sizeof(b));      //dijkstra 最短路
27     b[s] = true;                                //Source point 
28      C [S] = 0 ; 
 29      for (I = . 1 ; I <= N- . 1 ; I ++ )
 30      {
 31 is          minL = Maxx;                         // greedy find the minimum 
32          K = 0 ;
 33 is          for (J = . 1 ; J <= n-; J ++)      // look for points that can be updated 
34 is             IF ((B [J]!) && (C [J] < minL))
 35              {
 36                  minL = C [J];
 37 [                  K = J;
 38 is             }
39         if (k == e) break;     //小优化
40         b[k] = true;
41         for (j = 1; j <= n; j++)
42            if (c[k] + f[k][j] < c[j]) 
43              c[j] = c[k] + f[k][j];
44     }    
45    printf("%.2lf\n",c[e]);
46    return 0;
47 }

After the stack optimized Dijkstra algorithm is O (NlogN) 

 for (I = . 1 ; I <= N- . 1 ; I ++ ) 
    { 
        minL = Maxx; 
        K = 0 ;
         for (J = . 1 ; J <= n-; J ++) // look can update point 
           IF ((B [! J]) && (C [J] < minL)) 
            { 
                minL = C [J]; 
                K = J; 
            } 
        IF (K == E) BREAK ; 
        B [K] = to true ;
         for (J = . 1; j <= n; j++)
           if (c[k] + f[k][j] < c[j]) 
             c[j] = c[k] + f[k][j];
    }    

Optimization adjacency table plus heap

Another function can also be given a

3. the Bellman-Ford algorithm is O (NE)

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     double a[101][3],dis[1001],w[1001],min1;
 6     int n,m,x,y,k,f[1001][3],s,t;  //  f数组储存第i条边的起点与终点
 7     bool b[101];
 8     cin>>n;
 9     for (int i=1;i<=n;i++) 
10        scanf("%lf%lf",&a[i][1],&a[i][2]);
11     cin>>m;
12     for (int i=1;i<=m;i++)                       //初始化数组dis
13        dis[i]=0x7fffffff/3;
14  for (int i=1;i<=m;i++)
15     {
16         scanf("%d%d",&x,&y);
17         f[i][1]=x; f[i][2]=y;
18         w[i]=sqrt(pow(a[x][1]-a[y][1],2)+pow(a[x][2]-a[y][2],2));
19     }
20     cin>>s>>t;
21     dis[s]=0; 
22     for (int i=1;i<=n;i++) //ford算法主体
23        for (int j=1;j<=m;j++)
24        {
25           if (dis[f[j][1]]+w[j]<dis[f[j][2]]) dis[f[j][2]]=dis[f[j][1]]+w[j];
26           if (dis[f[j][2]]+w[j]<dis[f[j][1]]) dis[f[j][1]]=dis[f[j][2]]+w[j];
27        }
28     printf("%.2f",dis[t]);
29 }

4、SPFA算法O(kE)

 

Guess you like

Origin www.cnblogs.com/ljy-endl/p/11266426.html