The 2019 Asia Yinchuan First Round Online Programming F. Moving On

t topic Link: https://nanti.jisuanke.com/t/41290

Ideas: it is easy to think of the meaning of the title floyd, but due to the limitations of risk, how can we run floyd it.

Beginning misunderstood subject, thinking u-> v includes the end point to the starting point can not exceed the degree of risk, but look at the sample, it seems only the middle of the city can not exceed the risk.

We can think, every city has a risk, but the nature of floyd algorithm is i to j after the transfer of the first k cities, to get multi-source shortest path, then we might

Record number and risk city, then the city sorted by the degree of risk, renumbered Xianpao Floyd small risk, and f [k] [i] [j] denotes the k shortest path through cities,

Then we obtain f [k] [i] [j] = min (f [k-1] [i] [j], f [k-1] [i] [num [k]] + f [k- 1] [num [k] [j]), the degree of risk to low risk of pushing large.

For each inquiry, we only need to traverse the risk numbered n cities ran out of f [k] [i] [j], down through the city to the risk that is beyond the upper limit of risk f [k] [ u] [v],

Is our answer.


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
using namespace std;

typedef long long LL;
#define inf 1e9
#define rep(i, j, k) for (int i = (j); i <= (k); i++)
#define rep__(i, j, k) for (int i = (j); i < (k); i++)
#define per(i, j, k) for (int i = (j); i >= (k); i--)
#define per__(i, j, k) for (int i = (j); i > (k); i--)

const int N = 210;
int f[N][N][N];
int n,q;
int u,v,w;

struct node{
    int id;
    int ATK;

    friend bool operator<(const node& a,const node& b){
        return a.ATK < b.ATK;
    }
}W[N];

int main(){

    int T;
    scanf("%d",&T);

    rep(o,1,T){
        scanf("%d%d",&n,&q);

        rep(i,1,n){
            W[i].id = i;
            scanf("%d",&W[i].ATK);
        }

        sort(W + 1,W + n + 1);

        rep(i,1,n) rep(j,1,n){
            if(i == j) f[0][i][j] = 0;
            else f[0][i][j] = inf;
        }

        rep(i,1,n) rep(j,1,n){
            scanf("%d",&w);
            f[0][i][j] = w;
        }

        // rep(i,1,n) cout << W[i].ATK << endl;

        // rep(i,1,n){
        //     rep(j,1,n) cout << f[0][i][j] << " ";
        //     cout << endl;
        // }

        rep(k,1,n) rep(i,1,n) rep(j,1,n){
            f[k][i][j] = min(f[k-1][i][j],f[k-1][i][W[k].id]+f[k-1][W[k].id][j]);
        }

        printf("Case #%d:\n",o);

        int tmp;
        rep(i,1,q){
            scanf("%d%d%d",&u,&v,&w);
            tmp = 0;
            rep(j,1,n) if(W[j].ATK <= w) tmp = j;
            printf("%d\n",f[tmp][u][v]);
        }
    }

    getchar();getchar();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/SSummerZzz/p/11468549.html