P1613 foot problem solution

Today, there's really nothingThen I'll sell Meng bar (~ ¯ ▽ ¯) ~

Luogu

A thinking problemIt may be only for me, for the bigwigs are evident


Simplify the meaning of problems

Give you a map, find a path to make it bit paths \ (1 \) minimum number of

analysis

This topic question, there are two pitPerhaps only for me

  1. Every time to go, only you can stop at the endpoint, that is, if there is a road from \ (i \) to \ (J \) , if its path length of 7, not from \ (i \) go directly to \ (J \) , you go \ (3 \) times.

  2. Since \ (1 \) so directly run the shortest path is not necessarily the best.

Then think of solution

First, since the label ~ / path is too long special / cow * ~~ machine of the way on foot, so we thought of using multiplication to solve this problem

Then consider this last question is the most short-circuit requirements. We can see the data range \ (the n-<= 50 \) , so I think Freud, find the shortest path. However, due to the above conclusions, we can not directly run the shortest, and therefore we need to do a conversion on this map. This question is difficult to make the conversion to the point figure, so we chose to make the conversion to edge / path.

Then consider the transformation of the path. The reason can not directly run the Shortest Path before considering: We can not determine how to direct the path of binary bits in a relatively short period of time \ (1 \) the number of less, so when we put it into the path of contribution in binary in \ (1 \) the shortest number you can count.

Therefore, first pre-processing from \ (I \) to \ (J \) shortest path (1 \) \ number, and then seek the shortest side of the like.

For seeking \ (I \) to \ (J \) shortest path \ (1 \) number, since \ (I \) to \ (J \) path has a propertyOh, I do not know the specific name: \ (I \) to \ (T \) in the number of \ (K \) , \ (T \) to \ (J \) in the number of \ (K \) , then the \ (I \) to \ (J \) of \ (1 \) number is \ (K + 1 \) . Therefore, we can use similar transmitted shortest way between each two points to find closure in bins \ (1 \) number


code

#include<iostream>
#include<cstring>
using namespace std;
int n,m,x,y,dis[55][55];
bool map[55][55][65];
inline void pre(){
    for(register int t=0;t<=64;t++)
        for(register int k=1;k<=n;k++)
            for(register int i=1;i<=n;i++)
                for(register int j=1;j<=n;j++)
                    if(map[i][k][t-1]&&map[k][j][t-1])
                        map[i][j][t]=1,dis[i][j]=1;
}
inline void floyed(){
    for(register int k=1;k<=n;k++)
        for(register int i=1;i<=n;i++)
            for(register int j=1;j<=n;j++)
                dis[i][j]=min(dis[i][k]+dis[k][j],dis[i][j]);
}
int main(){
    scanf("%d%d",&n,&m);
    memset(dis,0x3f,sizeof(dis));
    for(register int i=1;i<=m;i++)
        scanf("%d%d",&x,&y),
        dis[x][y]=1,map[x][y][0]=1;
    pre();floyed();printf("%d",dis[1][n]);
}

Finally, international practice, thankyou for your attention

Guess you like

Origin www.cnblogs.com/fallen-down/p/11589151.html