Los Valley 1613, run.

Source: Internet
Author: User
Los Valley 1613, run.

Address: http://www.luogu.org/problem/show?pid=1613

Topic description

Small A's work is not only cumbersome, more stringent requirements, 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 and output formats

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 on a line representing the minimum number of seconds to the company.

Input and Output sample

Enter Sample #:

4 4

1 1

1 2

2 3

3 4

Output Sample #:

1

Description

"Sample Interpretation"
1->1->2->3->4, the total path length is 4-kilometer, the direct use of an escape device can be.
"Data Range"
50% data satisfies the optimal solution path length <=1000;
100% of the data satisfies the n<=50,m<=10000 and the optimal solution path length is <=maxlongint.

Ideas

Multiply the construction edge + shortest path.

The machine 1s can run 2^k, only requires that each node 1s can reach the node and then re-composition can be converted to the shortest path problem.

Multiplication method: Set G[u][v][k] to indicate whether there is a 2^k between the U-V to connect with each other, then there is a transfer:

G[U][V][K]=G[U][I][K-1] && G[i][v][k-1]

Shortest circuit: can use Floyd in O (n^3) time solution, can also use BFS in O (n^2) time to complete the task.

Code

 1 #include <iostream> 2 #include <cstring> 3 using namespace std;
 4 5 const int MAXN = 50+10;
 6 const int max_d = 64;
 7 8 int n,m;
9 int F[MAXN][MAXN];
a bool g[maxn][maxn][max_d+1]; one int main () {Ios::sync_with_stdio (false), cin>>n>>m; for (int i=1;i<=n;i++) for ( 
int j=1;j<=n;j++) f[i][j]=100;
int u,v;     for (int i=1;i<=m;i++) {cin>>u>>v; f[u][v]=1 g[u][v][0]=true 21           k=1;k<=max_d;k++ for (int i=1;i<=n;i++) in for (int t=1;t<=n;t++) 25                      for (int j=1;j<=n;j++)-if (G[i][t][k-1] && g[t][j][k-1]) 27 {28
G[i][j][k]=true;
F[i][j]=1; for (int k=1;k<=n;k++) (int i=1;i<=n;i++) A for (int j=1;j<=n;  J + +) F[i][j]=min (F[i][j],f[i][k]+f[k][j]); The more FloydThe community must pay attention to the cout<<f[1][n];
return 0; 37}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.