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

t题目链接:https://nanti.jisuanke.com/t/41290

思路:题目意思很容易想到floyd,但是由于危险度的限制,我们该怎么跑floyd呢。

一开始理解错题目了,以为u->v包括终点起点都不能超过给的危险度,不过看样例,好像只需要中间的城市不能超过危险度。

我们可以这么想,每个城市都有一个危险度,而floyd算法的本质是i到j经过前k个城市的转移,得到多源最短路,那么我们不妨

记录城市的编号和危险度,然后按城市的危险度排序,重新编号,危险度小的先跑floyd,然后f[k][i][j]表示经过前k个城市的最短路,

那么我们得出  f[k][i][j] = min(f[k-1][i][j],f[k-1][i][num[k]] + f[k-1][num[k][j]),从危险度小的推到危险度大的。

对于每个询问,我们只需要遍历按危险度编号的n个城市跑出的f[k][i][j],直到遍历到城市的危险度超出了危险度上限的那个f[k][u][v],

就是我们的答案了。


#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;
}

原文地址:https://www.cnblogs.com/SSummerZzz/p/11468549.html

时间: 2024-08-30 03:27:18

The 2019 Asia Yinchuan First Round Online Programming F. Moving On的相关文章

Fire-Fighting Hero (The 2019 Asia Nanchang First Round Online Programming Contest)

This is an era of team success, but also an era of heroes. Throughout the ages, there have been numerous examples of using the few to defeat the many. There are VV(Numbers 11 to VV) fire-fighting points in ACM city. These fire-fighting points have EE

Pangu Separates Heaven and Earth(签到题)(The 2019 Asia Nanchang First Round Online Programming Contest)

Long long ago, the sky and the earth were not separated, and the universe was chaotic. There was a giant named Pangu who slept for eighteen thousand years in this chaos. One day, Pangu woke up suddenly. When he saw the darkness around him, he took up

H. The Nth Item(The 2019 Asia Nanchang First Round Online Programming Contest)

题意:https://nanti.jisuanke.com/t/41355 给出N1,计算公式:A=F(N)Ni=Ni-1 ^ (A*A),F为类斐波那契需要矩阵快速幂的递推式. 求第k个N. 思路: 发现从大约1e5个数开始N交替出现,到一定位置%2即可.(or正解:https://blog.csdn.net/qq_41848675/article/details/100667808   or https://blog.csdn.net/jk_chen_acmer/article/detail

The 2019 Asia Nanchang First Round Online Programming Contest E. Magic Master

题目链接:https://nanti.jisuanke.com/t/41352 题目意思还是好理解的,看过的人不多,感觉是被通过量吓到了.其实就是个水题,反向模拟就好了, 用队列模拟,反向模拟,它要放m张卡到后面,那我就放m张卡到前面,一开始队列只有1张卡,慢慢加到n张卡, 先加大的卡,再一直到1的卡.对了,可能会出现只有5张卡,m却是6,7,8或9,10,那么为了减少不必要的模拟, 用mod来减少,因为有些模拟会让卡和之前比较,算是原封不动. 1 #include <iostream> 2

The 2019 Asia Nanchang First Round Online Programming Contest The Nth Item

The Nth Item 思路: 先用特征根法求出通向公式,然后通向公式中出现了\(\sqrt{17}\),这个可以用二次剩余求出来,然后可以O(\(log(n)\))求出. 但是还不够,我们先对\(n\)欧拉降幂,然后求base为\(\sqrt{1e9}\)的快速幂,预处理一些东西,就可以类似O(1)求出了. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #include<bits/std

The 2019 Aisa Nanchang First Round Online Programming Contest ——H

思路:矩阵快速幂+map 代码: #include<cmath> #include<algorithm> #include<cstdio> #include<map> const int INF=0x3f3f3f3f; const int maxn = 1e7+10; const int mod=998244353; using namespace std; typedef long long ll; map<ll,ll> mp; ll n; s

[翻译] Programming F#

最近,空军和我试译 Microsoft F#团队的 Chris Smith 所著的 Programming F# 一书.                版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载.

2019 ICPC Asia Yinchuan Regional

目录 Contest Info Solutions A. Girls Band Party B. So Easy D. Easy Problem E. XOR Tree F. Function! G. Pot!! H. Delivery Route I. Base62 K. Largest Common Submatrix N. Fibonacci Sequence Contest Info Practice Link Solved A B C D E F G H I J K L M N 9/1

The Preliminary Contest for ICPC Asia Yinchuan 2019

目录 Solutions Link Solutions 题意: 思路: 代码: [] 原文地址:https://www.cnblogs.com/c4Lnn/p/12333659.html