Minimum turnaround -- traversal of Graphs

Original Xiaoming and Xiaogang are flying to travel together. They are now located in No. 1 city. Their goal is No. 5 city. However, No. 1 city has no...

Original

Xiaoming and Xiaogang are flying to travel together. They are now located in No. 1 city. Their goal is No. 5 city. However, No. 1 city has no direct flight to No. 5 city.

But Xiaoming has collected a lot of flight information. Now Xiaoming hopes to find a way to take the flight, so that the number of transfers is the least.

input

The first line has four integers v, e, n and m, which respectively represent the vertex number, edge number, starting point and destination;

Next line e, each line outputs two integers, representing which two cities have direct flights;

output

Minimum transfer times

BFS -- once the destination is found, the number of transfers is the least, because BFS enumerates all the vertices that can be reached in N steps

import java.util.*; //BFS Once found, the minimum number of transfers public class Minimum turnaround { static int v; static int e; static int start; static int end; static int matrix[][]; static int que[]; static int que_step[]; static int book[]; static int flag=0; //sign public static void main(String[] args) { Scanner reader=new Scanner(System.in); v=reader.nextInt(); e=reader.nextInt(); start=reader.nextInt(); end=reader.nextInt(); matrix=new int[v+1][v+1]; que=new int[v+1]; que_step=new int[v+1]; book=new int[v+1]; //Matrix initialization for(int i=1;i<=v;i++) { book[i]=0; for(int j=1;j<=v;j++) { if(i==j) { matrix[i][j]=0; } else { matrix[i][j]=99999; } } } //Reading edge for(int i=1;i<=e;i++) { int one_City=reader.nextInt(); int two_City=reader.nextInt(); //Undirected graph matrix[one_City][two_City]=1; matrix[two_City][one_City]=1; } int head=1; int tail=1; //Initial location in queue que[tail]=start; que_step[tail]=0; book[start]=1; tail++; while(head<tail) { for(int i=que[head];i<=v;i++) { if(matrix[ que[head] ][i]>0 && matrix[ que[head] ][i]<99999 && book[i]==0) { //Queue entry que[tail]=i; que_step[tail]=que_step[head]+1; book[i]=1; tail++; //Destination judgment if(i==end) { flag=1; break; } } } if(flag==1) { break; } head++; } System.out.println(que_step[tail-1]); } }

DFS -- retrospective comparison

import java.util.*; public class Minimum turnaround DFS { static int v; static int e; static int start; static int end; static int matrix[][]; static int book[]; static int min=99999; static void dfs(int cur,int dis) { if(dis>min) { return; } if(cur==end) { if(dis<min) { min=dis; } return; } for(int i=1;i<=v;i++) { if(matrix[cur][i]>0 && matrix[cur][i]<99999 && book[i]==0) { book[i]=1; dfs(i,dis+1); book[i]=0; } } } public static void main(String[] args) { Scanner reader=new Scanner(System.in); v=reader.nextInt(); e=reader.nextInt(); start=reader.nextInt(); end=reader.nextInt(); matrix=new int[v+1][v+1]; //Numbering starts at 1 book=new int[v+1]; //Matrix initialization for(int i=1;i<=v;i++) { book[i]=0; for(int j=1;j<=v;j++) { if(i==j) { matrix[i][j]=0; } else { matrix[i][j]=99999; } } } //Reading edge for(int i=1;i<=e;i++) { int one_City=reader.nextInt(); int two_City=reader.nextInt(); matrix[one_City][two_City]=1; matrix[two_City][one_City]=1; } book[start]=1; dfs(1,0); System.out.println(min); } }

Test case:

Input:

5 7 1 5
1 2
1 3
2 3
2 4
3 4
3 5
4 5

Output:

2

14:38:13

2018-07-24

31 January 2020, 14:41 | Views: 3948

Add new comment

For adding a comment, please log in
or create account

0 comments