[JAVA] Didi-2021 School Recruitment Online Written Test-DE Data Development Test Paper-0913

Preface

Brothers will have offers, don’t panic, Aoli will give

Insert picture description here
Insert picture description here

1. D Star Islands (Those who have all A, 0.82 for A, consider it: four islands, now there are two bridges 1-2 3-4)

Time limit: 3000MS
Memory limit: 589824KB

Title description:
D Star Islands consists of n small islands. In order to strengthen the communication between the residents of the small island, the boss decided to start a bridge construction project to connect all n islands together.

Due to the impact of the financial crisis, the boss required the total cost of building the bridge to be the least, and also stipulated that the cost of each bridge should not exceed 10,000 D stars.

It should be noted that due to the geographical environment and climate, there is no way to directly build bridges between some small islands.

Now give you the cost data of the bridge construction between m islands and the value of k. Can this magnificent bridge construction project be successfully completed?

Note: There may be insufficient margins or overspends.

Input description
Multi-group input, input a positive integer T in the first line to indicate the group number of input data.
For each set of input data: enter m+1 rows.
The first line contains three positive integers, representing n, m, and k, respectively, where n≤100, m≤1000, and k≤10000. The three numbers are separated by spaces.
The next m rows represent the cost data of bridge construction between m islands, each row contains three positive integers, representing the number of the two islands (starting from 1) and the cost of bridge construction between the two islands. (Unit: ten thousand).

Output description For
each group of input data, if the bridge construction project can be completed, output "Yes", otherwise output "No".

Sample input
2
3 3 400
1 2 200
1 3 300
2 3 500
3 3 400
1 2 500
1 3 600
2 3 700

Sample output
Yes
No

Code:

package didi;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 
 * @Email: 
 * @Date: 2020/9/13
 * @Time: 19:43
 * @Version: 1.0
 * @Description: Description
 */
public class First3 {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        for (int l = 0; l < T; l++) {
    
    
            int n = scanner.nextInt();
            int m = scanner.nextInt();
            int k = scanner.nextInt();
            ArrayList<Integer[]> bridges = new ArrayList<>();
            for (int i = 0; i < m; i++) {
    
    
                Integer[] bridge = new Integer[2];
                for (int j = 0; j < bridge.length; j++) {
    
    
                    bridge[j] = scanner.nextInt();
                }
                if (scanner.nextInt() <= k) {
    
    
                    bridges.add(bridge);
                }
            }
            /*if (bridges.size() < n - 1) {//三个岛最少也得有两座桥
                System.out.println("No");
            } else {
                System.out.println("Yes"); //透风小技巧 只写这部分,不写下面的能A36%
            }*/
            HashSet<Integer> connections = new HashSet<Integer>();
            if (!bridges.isEmpty()) {
    
    
                connections.add(bridges.get(bridges.size() - 1)[0]);
                connections.add(bridges.get(bridges.size() - 1)[1]);
                bridges.remove(bridges.size() - 1);
            }
            while (true) {
    
    
                boolean end = true;
                for (int i = bridges.size() - 1; i > -1; i--) {
    
    
                    if (connections.contains(bridges.get(i)[0])
                            || connections.contains(bridges.get(i)[1])) {
    
    
                        connections.add(bridges.get(i)[0]);
                        connections.add(bridges.get(i)[1]);
                        bridges.remove(i);
                        end = false;
                    }
                }
                if (end) {
    
    
                    break;
                }
            }
            if (connections.size() == n) {
    
    
                System.out.println("Yes");
            } else {
    
    
                System.out.println("No");
            }
        }
        scanner.close();
    }
}


2. Graduation trip (all A)

Time limit: 3000MS
Memory limit: 589824KB

Title description:
Xiaodi is planning his graduation trip. He plans to find his foreign netizens. The first stop is Paris, France, but there are many routes to Paris, and there are many transportation options.

Now there is an undirected graph with the number of nodes n and the number of edges m representing all the routes of Xiaodi to Paris. Among them, Xiaodi’s home is node s, Paris is node e, and Xiaodi’s departure time is start. When will Xiaodi arrive in Paris at the earliest?

Input description
Single group input, separated by spaces between numbers.

Two integers in the first line: the number of nodes n, the number of edges m (n<=1000, m<=10000).

There are three integers in each row from the second line to the m+1th line: node u, node v, and the required time time (1<=u, v<=n, time<50, time is in hours).

The last line of the home: s, Paris location: e, departure time start (1<=s, e<=n, the departure time format is month.day/hour, the hour is a 24-hour system, and the departure year defaults to 2020 Year, and will definitely arrive in 2020, the data is guaranteed to have a solution.)

Output description The
earliest time e time to arrive in Paris (the format is the same as that of the departure time).

Sample input
4 4
1 2 5
1 3 6
2 4 8
3 4 6
1 4 7.9/8

Sample output
7.9/20

Prompt for
input example 2
4 4
1 2 25
1 3 18
2 4 28
3 4 22
1 4 7.9/8

Sample output 2
7.11/0

Code:

package didi;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 张志浩 Zhang Zhihao
 * @Email: [email protected]
 * @Date: 2020/9/13
 * @Time: 20:13
 * @Version: 1.0
 * @Description: Description
 */


public class Second2 {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
    
    
            int n = sc.nextInt();
            int m = sc.nextInt();
            List<int[]>[] ways = new List[n + 1];
            for (int i = 0; i < n + 1; i++) {
    
    
                ways[i] = new ArrayList<>();
            }
            for (int i = 0; i < m; i++) {
    
    
                int l = sc.nextInt();
                int r = sc.nextInt();
                int cost = sc.nextInt();
                int[] lr = {
    
    r, cost};
                ways[l].add(lr);
                int[] rl = {
    
    l, cost};
                ways[r].add(rl);
            }
            int start = sc.nextInt();
            int tar = sc.nextInt();
            String time = sc.next();

            int[] arrived = new int[n + 1];
            Arrays.fill(arrived, Integer.MAX_VALUE);
            Deque<int[]> list = new LinkedList<>();
            list.add(new int[]{
    
    start, 0});
            while (!list.isEmpty()) {
    
    
                int[] cur = list.poll();
                int pos = cur[0];
                int cost = cur[1];
                if (cost < arrived[pos]) {
    
    
                    arrived[pos] = cost;
                    for (int[] arr : ways[pos]) {
    
    
                        if (arr[1] + cost < arrived[arr[0]]) {
    
    
                            list.add(new int[]{
    
    arr[0], arr[1] + cost});
                        }
                    }
                }
            }
            int res = arrived[tar];
            String out = help(res, time);
            System.out.println(out);
        }
    }

    static int[] months = {
    
    0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static String help(int res, String time) {
    
    
        String[] temp = time.split("\\.");
        int month = Integer.parseInt(temp[0]);
        temp = temp[1].split("/");
        int day = Integer.parseInt(temp[0]);
        int hour = Integer.parseInt(temp[1]);

        hour += res;
        if (hour >= 24) {
    
    
            day += hour / 24;
            hour = hour % 24;
        }
        while (day > months[month]) {
    
    
            day -= months[month];
            month++;
        }
        StringBuilder arrive = new StringBuilder();
        arrive.append(month).append(".")
                .append(day).append("/").append(hour);

        return arrive.toString();
    }
}


Guess you like

Origin blog.csdn.net/weixin_43124279/article/details/108568045