Template - Floyd

void Floyd(){
    for(int k = 1; k <= n; ++k) {
        for(int i = 1; i <= n; ++i) {
            for(int j = 1; j <= n; ++j) {
                dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
            }
        }
    }
}

This means that the use of k as a transit point to try to get a new detour the shortest distance between two points from k.

It uses only k of them, you can get without going through other points of the shortest effect. Hungary looks a bit feeling algorithm?

https://www.luogu.org/problem/P1119

Point k is added chronologically, shortest update.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int MAXN = 205;

int n, m, t[MAXN];
int dis[MAXN][MAXN];

const int INF = 0x3f3f3f3f;

int main() {
#ifdef Yinku
    freopen("Yinku.in", "r", stdin);
#endif // Yinku
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            dis[i][j] = INF;
    for(int i = 1; i <= n; ++i)
        dis[i][i] = 0;
    for(int i = 1; i <= n; ++i)
        scanf("%d", &t[i]);

    for(int i = 1; i <= m; ++i) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        ++u,++v;
        dis[u][v] = w;
        dis[v][u] = w;
    }
    int q;
    scanf("%d", &q);
    int curti = 1;
    while(q--) {
        int u, v, T;
        scanf("%d%d%d", &u, &v, &T);
        ++u,++v;
        while(curti <= n && t[curti] <= T) {
            for(int i = 1; i <= n; ++i) {
                for(int j = 1; j <= n; ++j) {
                    dis[i][j] = min(dis[i][j], dis[i][curti] + dis[curti][j]);
                }
            }
            ++curti;
        }
        if(t[u] > T || t[v] > T)
            puts("-1");
        else
            printf("%d\n", dis[u][v] < INF ? dis[u][v] : -1);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Yinku/p/11345235.html