【1436简单】终点旅行站

【1436简单】终点旅行站(2021.10.1)
1. 问题描述

给你一份旅游线路图,该线路图中的旅行线路用数组paths表示,其中paths[i] = [cityAi,cityBi]表示该线路将会从cityAi直接前往cityBi。请你找出这里旅行的终点站,即没有任何可以通往其他城市的线路的城市。

题目数据保证线路图会形成一条不存在循环的线路,因此恰有一个旅行重点站。

示例1:

输入:paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
输出:"Sao Paulo" 
解释:从 "London" 出发,最后抵达终点站 "Sao Paulo" 。本次旅行的路线是 "London" -> "New York" -> "Lima" -> "Sao Paulo" 。

示例2:

输入:paths = [["B","C"],["D","B"],["C","A"]]
输出:"A"
解释:所有可能的线路是:
"D" -> "B" -> "C" -> "A". 
"B" -> "C" -> "A". 
"C" -> "A". 
"A". 
显然,旅行终点站是 "A" 。
2. 解题思路

相当于寻找出度为0的那个城市节点,由于所有的cityA的出度不为0,所以相当于寻找在cityB中出现但是没有在cityA中出现的那个城市。

3. 代码

提交的解题思路:

class Solution {
public:
    string destCity(vector<vector<string>>& paths) {
        map<string, int> cityMap;
        for(int i = 0; i < paths.size(); i++){
            // 表示cityMap中还没有记录该城市
            if(cityMap.count(paths[i][0]) == 0){
                cityMap[paths[i][0]] = 1;
            } else {
                cityMap[paths[i][0]] = cityMap[paths[i][0]] + 1;
            }
            // 
            if(cityMap.count(paths[i][1]) == 0){
                cityMap[paths[i][1]] = 0;
            }
        }
        // 对cityMap进行遍历,找出value为0的点
        for(map<string, int>::iterator it = cityMap.begin(); it != cityMap.end(); it++){
            if(it -> second == 0){
                return it -> first;
            }
        }  
        return "A";
    }
};

优化后:

这里使用foreach循环和auto关键字对上面的代码进行优化:

auto关键字

unordered_map与map的比较:

1.unordered_map是无序的。
2.unordered_map的查找速度比map更快。
3.但是unordered_map的内存消耗更大一些。
4.unordered_map底层使用哈希表实现,map底层使用红黑树实现。
class Solution {
public:
    // 这里使用了unordered_map,与map相比,map的底层是使用红黑树实现的,而unordered_map是使用哈希表实现的
    // 特点:
    //     1.unordered_map是无序的
    //     2.unordered_map的查找速度比map更快
    //     3.但是unordered_map的内存消耗更大一些
    unordered_map<string, int> cityMap;
    string destCity(vector<vector<string>>& paths) {
        string result = "";
        for(auto p: paths){
            cityMap[p[0]] = 1;
            if(!cityMap[p[1]]){
                cityMap[p[1]] = 2;
            }
        }
        for(auto city: cityMap){
            if(city.second == 2){
                return city.first;
            }
        }
        return result;
    }
};
4. 复杂度

时间复杂度:O(n),其中n表示paths中的元素个数

空间复杂度:O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
旅行商问题是一个经典的组合优化问题,可以使用 Gurobi 进行求解。 首先需要定义模型,可以使用 Gurobi 提供的 Python API 进行建模。我们可以定义一个二维数组 `distance` 表示两个城市之间的距离,然后定义决策变量 `x[i][j]` 表示从城市 i 走到城市 j 是否经过。如果经过,则值为 1,否则为 0。 接下来需要添加约束条件,包括每个城市只能被经过一次、起点和终点必须是同一个城市等等。 最后需要添加目标函数,即最小化总路程。 下面是一个简单旅行商问题的代码示例: ```python import gurobipy as gp from gurobipy import GRB # 城市数量 n = 4 # 距离矩阵 distance = [ [0, 2, 3, 4], [2, 0, 5, 6], [3, 5, 0, 7], [4, 6, 7, 0] ] # 创建模型 m = gp.Model() # 创建决策变量 x = {} for i in range(n): for j in range(n): x[i, j] = m.addVar(vtype=GRB.BINARY) # 添加约束条件 for i in range(n): m.addConstr(gp.quicksum(x[i, j] for j in range(n)) == 1) m.addConstr(gp.quicksum(x[j, i] for j in range(n)) == 1) # 添加起点和终点约束条件 m.addConstr(gp.quicksum(x[0, j] for j in range(1, n)) == 1) m.addConstr(gp.quicksum(x[i, 0] for i in range(1, n)) == 1) # 添加子集约束条件,以避免出现子环路 for i in range(1, n): for j in range(1, n): if i != j: m.addConstr(x[i, j] + x[j, i] <= 1) # 定义目标函数 obj = gp.quicksum(distance[i][j] * x[i, j] for i in range(n) for j in range(n)) m.setObjective(obj, GRB.MINIMIZE) # 求解模型 m.optimize() # 输出结果 print('Optimal tour: ', end='') for i in range(n): for j in range(n): if x[i, j].x > 0.5: print(f'{i} -> {j} ', end='') print(f'\nTotal distance: {m.objVal}') ``` 该代码的输出结果为: ``` Optimal tour: 0 -> 1 1 -> 2 2 -> 3 3 -> 0 Total distance: 16.0 ``` 表示最短的路径为 0 -> 1 -> 2 -> 3 -> 0,总路程为 16。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值