Topics describe the work of description small A is not only cumbersome, but also harsh provisions, requiring small a every morning before 6:00 to arrive at the company, otherwise this month's wages clear zero. But little a depends on the bed's bad habit. So in order to keep their wages, small a bought a very cow B space getaway, can run 2^k kilometers per second (k is any number). Of course, this machine is stored with longint, so the total length of the run can not exceed maxlongint km. Small A's home to the company's road can be seen as a map, small a home for Point 1, the company is Point N, each edge length is 1-kilometer. Little a wants to wake up as late as possible every day, so you can help him calculate that he needs at least a few seconds to get to the company. Data guarantees 1 to n at least one path. Input output format input/output input format:
The first line of two integers n,m, representing the number of points and the number of edges.
The next m line is two digits per line u,v, representing a u to v edge.
Output format:
A number that represents the minimum number of seconds to the company. 4 4
1 1
1 2
2 3
3 4
1
Thinking: 2^k easy to think twice, think very simple. Note: INF do not take too large, easy to overrun.
#include <iostream> #include <cstring> #include <cstdlib> #include <algorithm
> #include <cstdio> #include <cmath> using namespace std;
const int maxn=65;
int N,M,F[MAXN][MAXN];
BOOL MAP[MAXN][MAXN][MAXN];
inline int get () {char c;while (!isdigit (C=getchar ()));
int V=c-48;while (IsDigit (C=getchar ())) v=v*10+c-48;
return v;
int main () {memset (map,0,sizeof (map));
N=get (); M=get ();
for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) F[I][J]=MAXN;
for (int i=1;i<=m;++i) {int x=get (), Y=get ();
Map[x][y][0]=1;
F[x][y]=1; for (int k=1;k<=maxn;++k) for (int p=1;p<=n;++p) for (int i=1;i<=n;++i) for (int j=1;j<=n;++j) {if (map[
I][p][k-1]&&map[p][j][k-1]) {map[i][j][k]=1;
F[i][j]=1; 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]);
printf ("%d\n", F[1][n]);
return 0; }