Meituan 2021 School Recruitment Written Test-Programming Questions (General Programming Test Questions, Session 1) 1. Xiaomei’s flower delivery route

Xiaomei is a flower courier of Meituan. Flowers are a kind of commodity with a very short shelf life, so they need to be delivered to customers as soon as possible. One of the company's requirements for riders is to plan the delivery route so that riders can finish all orders Walk as little distance as possible. (The rider takes all the flowers that need to be delivered when he starts delivery, and does not need to return to the flower shop after each order. The distance settlement is from the flower shop to the last customer, and the return from the last customer's home to the flower shop is not counted. The company ’s
performance evaluation of the rider depends on two indicators, one is the sum of the distances from the flower shop to all customer addresses, and the other is the actual distance traveled by the rider.
Assuming that the flower shop is always located at position 1, there are n-1 customers in total, and their numbers are 2~n. Let dis(i,j) represent the distance from position i to position j, that is, the
insert image description here
shortest distance that is calculated respectively and the actual distance traveled by the rider.
In order to simplify the problem, we constrain the n positions to form a tree, that is, only n-1 edges are connected to each other in it, and n points are guaranteed to be connected to each other.

Time limit: 1 second for C/C++, 2 seconds for other languages
​​Space limit: 256M for C/C++, 512M for other languages
​​Input description:
The first line of output contains a positive integer n, which is the total number of flower shops and customers. (1<=n<=30000)
There are n-1 lines next, each line has three integers u, v, w, indicating that there is a road with a distance of w between u and v. (1<=w<=1000)

Output description:
The output contains two integers, separated by a space, which respectively represent the sum of the distance from the flower shop to all customer addresses and the actual distance traveled by the rider.
Example 1
Input example:
5
1 2 3
1 3 1
1 4 2
2 5 1
Output example:
10 10

#include <climits>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
struct Node {
    
    

    int dis = 0;
    vector<pair<int, int>> edges;
};
vector<Node> nodes;
int dissum = 0;
int walksum=0;
int maxdis=0;
void dfs(int now, int pre) {
    
    
    // cout<<now<<pre<<endl;
    for (auto& edge : nodes[now].edges) {
    
    
        
        if (edge.first != pre) {
    
    
            walksum+=edge.second*2;
            int len = nodes[now].dis + edge.second;
            // if(len<nodes[edge.first].dis){
    
    
            nodes[edge.first].dis = len;
            // cout<<len<<endl;
            dissum += len;
            maxdis=max(maxdis,len);
            dfs(edge.first,now);
            // }
        }
        else continue;
    }
    return;
}

int main() {
    
    
    int n;
    cin >> n;
    nodes.resize(n + 1);

    for (int i = 1; i <n; i++) {
    
    
        int u, v, w;
        cin >> u >> v >> w;
        pair<int, int> tempu(v, w);
        nodes[u].edges.push_back(tempu);
        pair<int, int> tempv(u, w);
        nodes[v].edges.push_back(tempv);
    }
    nodes[1].dis = 0;
    //深搜查看每个顾客到花店的距离
    dfs(1, 0);

    cout<<dissum<<' '<<walksum-maxdis<<endl;
    return 0;
}
// 64 位输出请用 printf("%lld")

Guess you like

Origin blog.csdn.net/weixin_45184581/article/details/129464718