洛谷灾后重建

灾后重建

题目背景

B 地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响。但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车。换句话说,只有连接着两个重建完成的村庄的公路才能通车,只能到达重建完成的村庄。

题目描述

给出 B 地区的村庄数 N N N,村庄编号从 0 0 0 N − 1 N-1 N1,和所有 M M M 条公路的长度,公路是双向的。并给出第 i i i 个村庄重建完成的时间 t i t_i ti,你可以认为是同时开始重建并在第 t i t_i ti 天重建完成,并且在当天即可通车。若 t i t_i ti 0 0 0 则说明地震未对此地区造成损坏,一开始就可以通车。之后有 Q Q Q 个询问 ( x , y , t ) (x,y,t) (x,y,t),对于每个询问你要回答在第 t t t 天,从村庄 x x x 到村庄 y y y 的最短路径长度为多少。如果无法找到从 x x x 村庄到 y y y 村庄的路径,经过若干个已重建完成的村庄,或者村庄 x x x 或村庄 y y y 在第 t t t 天仍未重建完成,则需要返回 -1

输入格式

第一行包含两个正整数 N , M N,M N,M,表示了村庄的数目与公路的数量。

第二行包含 N N N个非负整数 t 0 , t 1 , … , t N − 1 t_0, t_1,…, t_{N-1} t0,t1,,tN1,表示了每个村庄重建完成的时间,数据保证了 t 0 ≤ t 1 ≤ … ≤ t N − 1 t_0 ≤ t_1 ≤ … ≤ t_{N-1} t0t1tN1

接下来 M M M行,每行 3 3 3个非负整数 i , j , w i, j, w i,j,w w w w为不超过 10000 10000 10000的正整数,表示了有一条连接村庄 i i i与村庄 j j j的道路,长度为 w w w,保证 i ≠ j i≠j i=j,且对于任意一对村庄只会存在一条道路。

接下来一行也就是 M + 3 M+3 M+3行包含一个正整数 Q Q Q,表示 Q Q Q个询问。

接下来 Q Q Q行,每行 3 3 3个非负整数 x , y , t x, y, t x,y,t,询问在第 t t t天,从村庄 x x x到村庄 y y y的最短路径长度为多少,数据保证了 t t t是不下降的。

输出格式

Q Q Q行,对每一个询问 ( x , y , t ) (x, y, t) (x,y,t)输出对应的答案,即在第 t t t天,从村庄 x x x到村庄 y y y的最短路径长度为多少。如果在第t天无法找到从 x x x村庄到 y y y村庄的路径,经过若干个已重建完成的村庄,或者村庄x或村庄 y y y在第 t t t天仍未修复完成,则输出 − 1 -1 1

样例 #1

样例输入 #1

4 5
1 2 3 4
0 2 1
2 3 1
3 1 2
2 1 4
0 3 5
4
2 0 2
0 1 2
0 1 3
0 1 4

样例输出 #1

-1
-1
5
4

提示

对于 30 % 30\% 30%的数据,有 N ≤ 50 N≤50 N50

对于 30 % 30\% 30%的数据,有 t i = 0 t_i= 0 ti=0,其中有 20 % 20\% 20%的数据有 t i = 0 t_i = 0 ti=0 N > 50 N>50 N>50

对于 50 % 50\% 50%的数据,有 Q ≤ 100 Q≤100 Q100

对于 100 % 100\% 100%的数据,有 N ≤ 200 N≤200 N200 M ≤ N × ( N − 1 ) / 2 M≤N \times (N-1)/2 MN×(N1)/2 Q ≤ 50000 Q≤50000 Q50000,所有输入数据涉及整数均不超过 100000 100000 100000
先用spfa算法尝试跑一下,A了6个点

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;
const int N = 210;
const int M = 210 * 210;

bool st[N];
int build[N];
int dis[N], e[M], ne[M], w[M], h[N], t[M], idx;
int add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, t[idx] = max(build[a], build[b]), ne[idx] = h[a], h[a] = idx++;
} // t[idx]表示该边在什么时刻有效
int spfa(int s, int d, int ti) //求ti时刻,s到d的距离
{
    memset(st, false, sizeof st);
    memset(dis, 0x3f, sizeof dis);
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    st[s] = true;

    while (q.size())
    {
        int ver = q.front();
        q.pop();
        st[ver] = false;

        for (int i = h[ver]; i != -1; i = ne[i])
        {
            if (t[i] > ti)
            {
                continue;
            } //说明此时此路还没建好
            int j = e[i];
            if (dis[j] > dis[ver] + w[i])
            {
                dis[j] = dis[ver] + w[i];
                if (!st[j])
                {
                    st[j] = true;
                    q.push(j);
                }
            }
        }
    }
    if (dis[d] == 0x3f3f3f3f)
        return -1;
    else
        return dis[d];
}
int main()
{
    memset(h, -1, sizeof h);
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        scanf("%d", &build[i]);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
        add(b, a, c);
    }
    int q;
    scanf("%d", &q);
    for (int i = 0; i < q; i++)
    {
        int x, y, t;
        scanf("%d%d%d", &x, &y, &t);
        cout << spfa(x, y, t) << endl;
    }
    return 0;
}

用spfa还是太暴力了一点,虽然简化了一点时间,但是思路上没有用到题目提供的一个关键点

第二行包含 N N N个非负整数 t 0 , t 1 , … , t N − 1 t_0, t_1,…, t_{N-1} t0,t1,,tN1,表示了每个村庄重建完成的时间,数据保证了 t 0 ≤ t 1 ≤ … ≤ t N − 1 t_0 ≤ t_1 ≤ … ≤ t_{N-1} t0t1tN1

看到题解以后,发现自己对于flody的理解还是太浅了,只是停留在背模板的阶段上,下面附上flody解法

#include <iostream>
#include <queue>
#include <cstring>

using namespace std;
const int N = 210;
int dis[N][N], build[N];
int ti;
int n, m;

void flody(int k) //用k来更新其他点
{

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
        {
            dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
}
int main()
{
    memset(dis, 0x3f, sizeof dis);
    for (int i = 0; i < n; i++)
        dis[i][i] = 0;
    cin >> n >> m;
    for (int i = 0; i < n; i++)
        scanf("%d", &build[i]);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        dis[a][b] = c;
        dis[b][a] = c;
    }
    int q;
    scanf("%d", &q);
    int now = 0;//计数点,从1-200
    for (int i = 0; i < q; i++)
    {
        int x, y, t;
        scanf("%d%d%d", &x, &y, &t);
        if (build[x] > t || build[y] > t)
        {
            cout << "-1" << endl;
            continue;
        }
        while (build[now] <= t && now < n)//因为重建时间是递增的,所以我们可以一个一个点更新过去
        {
            flody(now);
            now++;
        }
        if (dis[x][y] == 0x3f3f3f3f)
            cout << "-1" << endl;
        else
            cout << dis[x][y] << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值