Los Angeles P1119 Disaster Reconstruction - Floyd

Title: https://www.luogu.org/problemnew/show/P1119

N is very small, consider using Floyd;

Because t has been sorted, add points one by one, and Floyd can update it;

This also gives us an inspiration. If t is not sorted, it can also be processed offline and sorted again.

code show as below:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,q,f[205][205],t[205],nw;
int main()
{
    memset(f,0x3f,sizeof f);
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%d",&t[i]),f[i][i]=0;//
    for(int i=1,x,y,z;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        f[x][y]=f[y][x]=z;//
    }
    scanf("%d",&q);
    int x,y,tim;
    while(q--)
    {
        scanf("%d%d%d",&x,&y,&tim);
        while(nw<n&&t[nw]<=tim)
        {
            for(int j=0;j<n;j++)
                for(int k=0;k<n;k++)
                    f[j][k] =min(f[j][k],f[j][nw]+ f[nw][k]);
            nw++;
        }
        if(f[x][y]==0x3f3f3f3f||t[x]>tim||t[y]>tim)printf("-1\n");//
        else printf("%d\n",f[x][y]);
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324750687&siteId=291194637