Codevs 1062 Route Selection

Source: Internet
Author: User

1062 Route Selectiontime limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description Description

In network communication, the shortest path is often required. However, there is a problem with the shortest path transfer: If there is only one shortest path given between the two end nodes. The signal transmission is at risk of interruption in the event of any node or link failure in the path. As a result, the following improvements have been made to network routing:

Provides three paths for communication between any two nodes, i.e. shortest path, second shortest path, and third shortest path.

The first shortest path is defined as: given a network d={v,a,w} with no negative loops, where V={v1,v2,...,vn},a is a set of edges, a set of W weights, and P1 is the shortest (V1,VN) path in D. The P1 is called the shortest (V1,VN) path in D, and if there is a (V1,VN) road in D, the P2 meets the following conditions:

(1) P2≠p1; (2) d does not exist in the P1 of the road p, so that:

(3) W (P1) ≤w (P) <w (P2)

The second shortest path of D is called P2.

The third shortest path is defined as: Set P2 is the second shortest (V1,VN) path in D, if there is a (V1,VN) road in D P3 the following conditions are met:

(1) P3≠p2 and P3≠P1; (2) d does not have a path p that is different from P1,P2, which makes:

(3) W (P2) ≤w (P) <w (P3)

The P3 is called the third shortest path in D.

Now given a network of N nodes, N≤30, the first, second, and third shortest paths between the given two points are obtained.

Enter a description Input Description

Input: N S T Max (separated by a space between the values of each cell)

M11 M12 ... m1n

M21 M22 ... M2n

... ...

Mn1 Mn2 ... Mnn

where n is the number of nodes, S is the starting point, T is the end point, Max is an integer representing Infinity, mij describes the distance from I to J, and if Mij=max, it means that there is no direct path from I to J, Mii=0.

Output description Output Description

Output: Three paths (small to large output), one row per path, in the form of: path length starting point ... End point (separated by a space in the middle)

Sample input Sample Input

5 1 5 10000

0 1 3) 10000 7

10000 0 1) 10000 10000

10000 10000 0) 1 4

10000 10000 10000) 0 1

10000 1 10000) 10000 0

Sample output Sample Output

4 1 2 3 4 5

5 1 3) 4 5

6 1 2) 3 5

/*Dijkstra algorithm for short-circuit solving*/#include<cstdio>#defineN 50Const intinf=1000000009;intN;intd[n][n],dist[3][n],vis[3][n],step[3][n][n];voidDijkstraintS) {    inti,j,tm,u,v,k,l;  for(i=1; i<=n;i++)         for(j=0;j<3; j + +) Dist[j][i]=inf;  for(i=1; i<=n;i++)        if(I!=s&&d[s][i]<inf) dist[0][i]=d[s][i],step[0][i][++step[0][i][0]]=R; dist[0][s]=0; step[0][s][0]=0; vis[0][s]=1;  for(i=1; i<n*3; i++){         for(j=1, tm=inf,u=v=1; j<=n;j++)             for(k=0;k<3; k++)                if(!vis[k][j]&&tm>dist[k][j]) {tm=dist[k][j];u=k;v=J;} VIS[U][V]=1;  for(j=1; j<=n;j++)            if(v!=j) {k=dist[u][v]+D[v][j]; if(!vis[0][j]&&k<=dist[0][j]) {dist[2][j]=dist[1][j]; step[2][j][0]=step[1][j][0];  for(l=1; l<=step[1][j][0];l++) step[2][j][l]=step[1][j][l]; dist[1][j]=dist[0][j]; step[1][j][0]=step[0][j][0];  for(l=1; l<=step[0][j][0];l++) step[1][j][l]=step[0][j][l]; dist[0][j]=K;  for(l=0; l<=step[u][v][0];l++) step[0][j][l]=Step[u][v][l]; step[0][j][++step[0][j][0]]=v; }Else if(!vis[1][j]&&k<dist[1][j]) {dist[2][j]=dist[1][j]; step[2][j][0]=step[1][j][0];  for(l=1; l<=step[1][j][0];l++) step[2][j][l]=step[1][j][l]; dist[1][j]=K;  for(l=0; l<=step[u][v][0];l++) step[1][j][l]=Step[u][v][l]; step[1][j][++step[1][j][0]]=v; }Else if(!vis[2][j]&&k<dist[2][j]) {dist[2][j]=K;  for(l=0; l<=step[u][v][0];l++) step[2][j][l]=Step[u][v][l]; step[2][j][++step[2][j][0]]=v; }            }    }}intMain () {ints,t,ig,i,j; scanf ("%d%d%d%d",&n,&s,&t,&IG);  for(i=1; i<=n;i++){         for(j=1; j<=n;j++) {scanf ("%d",&D[i][j]); if(D[i][j]==ig) d[i][j]=inf;    }} Dijkstra (S); printf ("%d", dist[0][t]);  for(i=1; i<=step[0][t][0];i++) printf ("%d", step[0][t][i]);p rintf ("%d\n", T); printf ("%d", dist[1][t]);  for(i=1; i<=step[1][t][0];i++) printf ("%d", step[1][t][i]);p rintf ("%d\n", T); printf ("%d", dist[2][t]);  for(i=1; i<=step[2][t][0];i++) printf ("%d", step[2][t][i]);p rintf ("%d\n", T); return 0;}

Codevs 1062 Route Selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.