灾后重建

P1119 灾后重建 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

  • floyd求最短路的应用
  • 对于每次询问,都会新增一批重建好的村子(每次询问时间都会增加,那么可能就会有新建好的村子,那么就可以用floyd根据这些新修好的村子(作为k)去更新所有的最短路径
// https://www.luogu.com.cn/problem/P1119
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 10001
const ll INF = 10e9;
ll n, m, tim[MAX], graph[MAX][MAX], q;
void init()
{
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            graph[i][j] = graph[j][i] = INF;
        }
    }
}
void input()
{
    cin >> n >> m;
    init();
    for (int i = 0; i < n; i++)
    {
        scanf("%d", tim + i);
    }
    while (m--)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        graph[a][b] = c;
        graph[b][a] = c;
    }
}

void work()
{
    cin >> q;
    int now_cun = 0;
    while (q--)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        while (tim[now_cun] <= c && now_cun < n)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (graph[i][j] > graph[i][now_cun] + graph[now_cun][j])
                    {
                        graph[i][j] = graph[j][i] = graph[i][now_cun] + graph[now_cun][j];
                    }
                }
            }
            now_cun++;
        }
        if (tim[a] > c || tim[b] > c || graph[a][b] == INF)
            printf("-1\n");
        else
            printf("%d\n", graph[a][b]);
    }
}
int main()
{
    input();
    work();
}

 

posted on 2022-08-11 09:38  樵风  阅读(49)  评论(0)    收藏  举报