Luo Gu P1613 foot
Topic links: Luo Gu P1613 foot
Algorithms DP
Tags: 图论
, ,倍增
topic
Topic background
A little work is not only cumbersome, more stringent requirement that every morning A small company arrives before 6:00, otherwise cleared salary this month. A small but happens to have a bad habit of stay in bed. So in order to keep his salary, bought a small A foot space is a very cow B, every second can run \ (2 ^ k \) one thousand meters ( \ (k \) is any natural number). Of course, this machine longint is stored, so the total foot length can not exceed maxlongint km. A small home to the company can be viewed as a way to view a small point A home, a company point n, the length of each edge are one thousand meters. A small day want to be able to wake up as late as possible, so let him help you calculate, he needs at least a few seconds to get to the company. A data path to ensure that at least 1 to n.
Title Description
The first line of two integers n, m, the number indicates the number of points and edges.
The next two lines each m numbers u, v, v represents a u to the edge.
Input Format
Line a number that represents the minimum number of seconds to the company.
Output Format
Conveying the least amount of output.
Sample input and output
Input # 1
4 4
1 1
1 2
2 3
3 4
Output # 1
1
Description / Tips
Sample [explain]
1-> 1-> 2-> 3-> 4, the total path length of 4 km and a foot control can be used directly.
【data range】
50% of the data satisfies the optimal solution path length <= 1000;
100% data satisfies n <= 50, m <= 10000, optimal solution path length <= maxlongint.
answer:
Starting from the title, when you see the title of " \ (2 ^ k \) " when you can think of this question to a skill - doubled .
Suppose we have some point, if the distance between two points where the sum may reach \ (n-^ \ 2) , then the distance between these two points is the cost \ (1 \) , so that the entire figure, All distance \ (2 ^ n \) edge weight of the edge between the pair of points is 1. The question then becomes how to ask the right side which between two points is 1 .
Here we use the DP to achieve multiplication, we use the outermost loop k=1->64
, represents power, the inner loop 2 i=1->n
, , j=1->n
, t=1->n
whether or not a point can be updated by an edge weight of 1 reaches the edge, thereby realizing multiplication process , specific code as follows:
for (int k = 1; k <= 64; k ++ )
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
for (int t = 1; t <= n; t ++ )
if (f[j][i][k - 1] && f[i][t][k - 1])
f[j][t][k] = dis[j][t] = 1;
After the data satisfying the title \ (n-\ Le 50 \) , so ultimately the shortest path can be used when the Floyd , three Solving the shortest cycle, the shortest path to the final output between from 1-n.
AC Code
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 70;
int n, m;
int f[N][N][N], dis[N][N];
int main()
{
memset(dis, 0x3f, sizeof dis);
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i ++ )
{
int x, y;
scanf("%d%d", &x, &y);
f[x][y][0] = 1;
dis[x][y] = 1;
}
for (int k = 1; k <= 64; k ++ )
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= n; j ++ )
for (int t = 1; t <= n; t ++ )
if (f[j][i][k - 1] && f[i][t][k - 1])
f[j][t][k] = dis[j][t] = 1;
for (int k = 1; k <= n; k ++ )
for (int i = 1; i <= n; i ++)
for (int j = 1; j <= n; j ++ )
dis[i][j] = min(dis[i][k] + dis[k][j], dis[i][j]);
printf("%d", dis[1][n]);
return 0;
}