Fzu 2090 travel Agency troubles Floyd to find the least ring of the graph without direction

Source: Internet
Author: User

Topic Link: The trouble of travel agency

Test instructions is the smallest loop of the graph, and if so, the number of outputs and the output weights.

Just mended a Floyd dynamic programming principle, using the idea of sliding arrays. So, this problem is the deformation of Floyd thought. In the process of k from 1 to n is updated to K, the Mindis array is saved only by 1~k-1 ordinal point, any two between the shortest path weights, this time, select Point K as the starting point of the ring as the end point, in [1, k) Select two points I, J to get a ring, the weight of the ring is Mindis[i ][J] + dis[i][k] + dis[j][k]. So the traversal is all the rings, and there are no duplicate points within the ring. Rings are not repeated. So ingenious ~ ~ to offer my knees ...

Attached code:

#include <stdio.h> #include <string.h> #include <iostream> #define INF 100000000using namespace std;    int Dis[110][110];int Mindis[110][110];int Main () {int t;    CIN >> T;        while (t--) {int n, m;        CIN >> n >> m;            for (int i=1, i<=n; ++i) {for (int j=1; j<=n; ++j) {dis[i][j] = inf;            }} for (int i=0; i<m; ++i) {int x, y, W;            Cin >> x >> y >> w;                if (Dis[x][y] > W) {dis[x][y] = W;            Dis[y][x] = W; }} for (int i=1, i<=n; ++i) {for (int j=1; j<=n; ++j) {mindis[i][j] = dis I            [j];        }} int ansdis = inf, anscnt = 0;                    for (int k=1, k<=n; ++k) {for (Int. I=1; i<k; ++i) {for (int j=i+1; j<k; ++j) {    if (Ansdis > Dis[i][k] + dis[j][k] + mindis[i][j]) {                    Ansdis = Dis[i][k] + dis[j][k] + mindis[i][j];                    anscnt = 1;                    } else if (Ansdis = = Dis[i][k] + dis[j][k] + mindis[i][j]) {anscnt++; }}} for (int i=1, i<=n; ++i) {for (int j=1; j<=n; ++J) {Mindis[i][j] = min (Mindis[i][j], mindis[i][k] + mindis[j][k]);//Feel the last mindis[j][k] if you switch to mindis[ K][J] The words should be WA's, however not.            So strange ...}        }} if (anscnt = = 0) {cout << " -1\n";    } else cout << ansdis << "<< anscnt << Endl; } return 0;}

Note: Mindis[j][k] should not be equal to mindis[k][j]. Puzzled...................

Fzu 2090 travel Agency troubles Floyd to find the least ring of the graph without direction

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.