[Explanations] P1119 reconstruction

I feel this question very clever ideas, clever use of \ (Floyd \) is thought to work out this problem, have to say, \ (A \) out this problem in the future, I feel myself to \ ( Floyd \) of the substance to know more.

Thinking

In fact, we all know that \ (Floyd \) A standard code so long

for(int k=1;k<=n;++k)  
  for(int i=1;i<=n;++i)  
    for(int j=1;j<=n;++j)  
      f[i][j]=min(f[i][j],f[i][k]+f[k][j]);  

So for this question, for every village, we have a time \ (time_i \) , only two villages connected to each route to fix a time just less than equal to the current inquiry time (Note: The inquiry time is guaranteed monotonic increasing), this route is valid.
For each repaired village \ (i \) , we can all attached \ (i \) Shortest village village update again, the code is implemented as

void update(int k)
{
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            f[i][j]=f[j][i]=min(f[i][j],f[i][k]+f[k][j]);
}

Yes, with the ordinary \ (Floyd \) is roughly the same, but we are given a separate update \ (k \) point
\ (Code: \)

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int tot,n,m;
int t[211],f[211][211];
bool book[211];
void update(int k)
{
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)
            f[i][j]=f[j][i]=min(f[i][j],f[i][k]+f[k][j]);
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    int inf=1e9;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;++i) scanf("%d",&t[i]);
    for(int i=0;i<n;++i)
        for(int j=0;j<n;++j)    
            f[i][j]=inf;
    for(int i=1;i<=m;++i)
    {
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        f[u][v]=f[v][u]=w;
    }
    int Q,now=0;
    scanf("%d",&Q);
    for(int i=1;i<=Q;++i)
    {
        int x,y,ta;
        scanf("%d %d %d",&x,&y,&ta);
        while(t[now]<=ta && now<n)
        {
            book[now]=true; 
            update(now);        
            ++now;
        }
        if(t[x]>ta || t[y]>ta || f[x][y]==inf) printf("-1\n");
        else printf("%d\n",f[x][y]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Call-me-zhz/p/11316115.html