Keywords: Java

# Huawei's 2019 autumn recruitment written test Java implementation (3)

Written test date: August 14, 2019

## Question three (300 points)

### Title Description

There is a social software APP, assuming that the registered user is m (0 < m < 50), the user number is 0~m-1, and r[i,j] is used to represent the friend relationship between user I and user J (r[i][j]=0 means that I and J are not friends, r[i][j]=1~9 means that they are friends and the value represents familiarity, and the more the number is, the more familiar they are, in which r[i][j]=r[j][i], 0 < = I, J < = m-1), try to program, output n degree friends of a specific user I (1 degree friends represent direct friends, 2 degree friends represent friends of friends, and so on 1 < = n < = 10), and output friends list in descending order of recommended value (friends with the same recommended value, in ascending order of friend number, where recommended value refers to the sum of relationship familiarity, for example, users I and j are friends and familiarity r[i][j]=4, j If K and I are friends and r[j][k]=6, and I and K are not friends, that is, r[i][j]=0, then user K is a 2-degree friend of user I and the recommended value is r[i][j]+r[j][k]=10). If this user I does not have n-degree friend output-1.

### Enter a description

Input an integer T, indicating that there are test data of group t (0 < T < 100).

For each group of test data input two lines,

i n the first line, input three integer numbers m,i,n respectively representing the number of users m, a user number i, n-degree friends, that is, representing the n-degree friends of the user i to be output in this group of tests,

In the second line, input an integer number k, and then 3*k integers are separated by spaces. There are no three relationships. Each relationship is composed of three integer arrays into i,j,w, which represents the familiarity of user I and j, that is, r[i][j]=w. the relationship without input is non friend by default (r[i][j]=r[j][i]=0).

### Output description

Output T lines, each line corresponds to the

N-degree friends of user i are output in descending order of recommended value, and friends with the same recommended value are output in ascending order of friend number. Numbers are separated by spaces. If there is no n degree friend output - 1.

### Example

input

```2
10 5 2
13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9
10 0 2
13 0 3 5 0 4 9 0 6 8 0 7 5 1 2 6 1 6 3 2 9 7 3 4 3 3 5 3 3 8 3 3 9 3 5 8 9 7 8 9
```

output

```7 0 4 9
1 5 8 9
```

### analysis

From the analysis of big questions, it must be breadth first (bfs).

Regardless of the recommended value, it is equivalent to listing all the friends that can arrive after n passes, and it is clearly stated in the stem that the friends of degree i and degree j are mutually exclusive (i < > j). Scan the friends that can be connected from the starting point, mark them as 1 degree friends, and then continue to deliver them from multiple 1 degree friends to 2 degree friends. Note that the marked points are not in the delivery range. And so on, you can get a list of n-degree friends.

Considering the recommended value, in fact, the friend value is accumulated continuously during the transfer, and can be saved with temporary variables.

### answer

``` // This writing method is complex, and only arrays can be used to implement it if you are interested in it
import java.util.*;

class f {
int i, sum;

public f(int i, int sum) {
this.i = i;
this.sum = sum;
}
}

public class Main_3 {
static List<f> flist = new ArrayList<>();

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int TT = sc.nextInt();
Main_3 m3 = new Main_3();
for (int ii = 0; ii < TT; ii++) {
int m = sc.nextInt(), i = sc.nextInt(), n = sc.nextInt(), k = sc.nextInt();
int[][] r = new int[m][m];
boolean[][] v = new boolean[m][m];
for (int j = 0; j < k; j++) {
int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
r[a][b] = c;
r[b][a] = c;
}
List<f> list = new ArrayList<>();
list.add(new f(i, 0));
m3.bfs(n, r, v, list);
if (list.size() == 0) {
System.out.println(-1);
continue;
}
Collections.sort(flist, new Comparator<f>() {
@Override
public int compare(f o1, f o2) {
if (o1.sum == o2.sum) {
return o1.i - o2.i;
} else {
return o2.sum - o1.sum;
}
}
});
for (int kkk = 0; kkk < flist.size() - 1; kkk++) {
System.out.print(flist.get(kkk).i + " ");
}
System.out.println(flist.get(flist.size() - 1).i);
}
sc.close();

}

public void bfs(int n, int[][] r, boolean[][] v, List<f> list) {
if (n == 0) {
flist = new ArrayList<>(list);
return;
}
List<f> list1 = new ArrayList<>();
for (int j = 0; j < list.size(); j++) {
for (int kkk = 0; kkk < r.length; kkk++) {
int temp = list.get(j).i;
if (!v[kkk][temp] && r[kkk][temp] != 0) {
v[kkk][temp] = true;
v[temp][kkk] = true;
list1.add(new f(kkk, list.get(j).sum + r[kkk][temp]));
}
}
}
bfs(n - 1, r, v, list1);
}
}
```

Code is not easy. Please indicate the source of reprint

Posted by sageman on Mon, 15 Jun 2020 00:48:17 -0700