Floyd + record path HDU 1385

Source: Internet
Author: User

Floyd algorithm:
It is used to solve the whole-source shortest path problem, which is the shortest path between any two points.
Define D[K][I][J] as the shortest path to J with I as the starting point for only using 0~k.
The situation of the 0~k is attributed to 0~k-1. When 0~k, it is divided into two cases, one is only K once, the other is not through K. If not through K, then d[k][i][j] = D[k-1][i][j]. If passed K, then d[k][i][j] = D[k-1][i][k]+d[k-1][k][j].
Transfer equation: d[k][i][j] = min (D[k-1][i][j],d[k-1][i][k]+d[k-1][k][j]).
Lenovo's approach to knapsack problem, we can directly use a two-dimensional array to store, continuous d[i][j] = min (d[i][j],d[i][k]+d[k][j]) update.
record path in Floyd: Recording precursor:

In Dijkstra, the record path typically uses a pre[] array to record the precursors of each point, updating the precursor while updating the shortest path.
Floyd can also do so, with pre[a][b] to indicate the beginning of a case, the precursor of point B. Initialize to traverse i=0->n,p[i][j] = i. The p[i][i] is assigned to-1, which makes it easy to restore the path. Record successor:

But a bit different is that you can also use P[a][b] to record the successor of Point A in the case where B is the end point. Floyd can record the successor, because after this definition, if you want to go through the K-point, then p[i][j] should be updated by P[i][k], that is, the end of the B, the successor of a point is actually the end of the K, a point of the successor. The initialization is then changed to I=0->n, p[i][j] = J. P[i][i] is also assigned to-1, to facilitate the restoration of the path.
hdu 1385
Test Instructions: Give a graph with multiple queries, which is the shortest path required between any two points. And the path is output. If there is the same shortest path, the output dictionary order is minimal (the ORZZ is not noticed in the beginning)
Background: Using the method of recording precursor to save the route, turn, WA, looked for a long time to find the dictionary order, so in d[i][j] = D[i][k]+d[k][j] should be careful, There may be a need to update the path, although the shortest circuit does not change. The dictionary sequence is not sufficient for the
to find the precursor of the record, because the precursor can only be as small as possible, but the dictionary order should start from scratch and each number is as small as possible.
discuss a set of data:
3
0 2 4
-1 0 1
-1-1 0
1 1 1
1 3
results are 1–>2–>3
Record the result will be 1–& Gt;3. The
then looked at others to learn the next discovery can also record the successor Orz. In the output of the direct sequential output is good. If the record is a precursor, you can use a stack to save first, and finally the stack of output.

Floyd #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include &
Lt;stack> using namespace std;
#define M #define INF 0x3f3f3f3f int d[m][m];
int P[m][m];//p[i][j] denotes the successor of I in the case of the end of J.
int v[m];
int n;
        void Floyd () {for (int k = 1;k <= n;k++) for (int i = 1;i <= n;i++) for (int j = 1;j <= n;j++) {
            if (D[i][j] > D[i][k]+d[k][j]+v[k])//after K points need to pay v[k] so much tax {d[i][j] = d[i][k]+d[k][j]+v[k];
        P[I][J] = p[i][k];
    } else if (d[i][j] = = D[i][k]+d[k][j]+v[k]) p[i][j] = min (p[i][j],p[i][k]);
            }} int main () {while (scanf ("%d", &n) ==1 && N) {for (int i = 1;i <= n;i++) {  for (int j = 1;j <= n;j++) {p[i][j] = j;
                The successor to the end of J is initialized to J scanf ("%d", &d[i][j]);
            if (d[i][j] = =-1) d[i][j] = INF;
        }} int A, b; FoR (int i = 1;i <= n;i++) {P[i][i] =-1;
        scanf ("%d", &v[i]);
        } Floyd ();
            while (scanf ("%d%d", &a,&b) ==2) {if (a==-1 && b==-1) break;
            printf ("From%d to%d: \ n", A, b);
            printf ("Path:");
            int i = p[a][b];
            printf ("%d", a);
                while (i! =-1) {printf ("-->%d", I);
            i = p[i][b];
        } printf ("\ntotal Cost:%d\n\n", d[a][b]);
}} return 0;
 }

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.