Differential constraints - 51Nod 1366 (inequality), 2019.10.25 T2

Face questions

51Nod 1366

solution

This is a simple differential constraint problem according to the meaning of problems FIG built by Floyd algorithm to the maximum length is obtained for the longest path.. \ (| XY | \ leqslant D \) , expands to
\ [\ begin { XY} Cases \ leqslant | D | \\ YX \ leqslant | D | \ Cases End {} \]
. because both negative positive number less than simply pressing the adjacency matrix can be built FIG, especially when implemented to be noted that FIG built Therefore, use the \ (0 \) to cover all sides right from the ring, and this problem can not be reflected in the sample solving.

The total time complexity \ (O (. 3) \ ^ TN) , the space complexity \ (O (^ n-2) \) .

program

#include <iostream>
#include <cstring>
using namespace std;

#define MAXN 52
#define INF 0x3f3f3f3f
int T, N, D, d[MAXN][MAXN];

void floyd() {
    for (int k = 0; k < N; k++)
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                if (d[i][k] + d[k][j] < d[i][j] && d[i][k] < INF && d[k][j] < INF)
                    d[i][j] = d[i][k] + d[k][j];
}

int main() {
    cin >> T;
    while (T--) {
        cin >> N >> D;
        for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++) {
            char c;
            cin >> c;
            d[i][j] = c == 'Y' ? D : INF;
        }
        for (int i = 0; i < N; i++)
            d[i][i] = 0;
        floyd();

        int mind = -INF;
        for (int i = 0; i < N; i++)
            for (int j = 0; j < N; j++)
                mind = max(mind, d[i][j]);
        if (mind >= INF)
            cout << -1 << endl;
        else
            cout << mind << endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lrw04/p/11795060.html