solution.md 6.9 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 重新安排行程
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<p>给你一份航线列表 <code>tickets</code> ,其中 <code>tickets[i] = [from<sub>i</sub>, to<sub>i</sub>]</code> 表示飞机出发和降落的机场地点。请你对该行程进行重新规划排序。</p>

<p>所有这些机票都属于一个从 <code>JFK</code>(肯尼迪国际机场)出发的先生,所以该行程必须从 <code>JFK</code> 开始。如果存在多种有效的行程,请你按字典排序返回最小的行程组合。</p>

<ul>
	<li>例如,行程 <code>["JFK", "LGA"]</code><code>["JFK", "LGB"]</code> 相比就更小,排序更靠前。</li>
</ul>

<p>假定所有机票至少存在一种合理的行程。且所有的机票 必须都用一次 且 只能用一次。</p>

<p> </p>

<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary1-graph.jpg" style="width: 382px; height: 222px;" />
<pre>
<strong>输入:</strong>tickets = [["MUC","LHR"],["JFK","MUC"],["SFO","SJC"],["LHR","SFO"]]
<strong>输出:</strong>["JFK","MUC","LHR","SFO","SJC"]
</pre>

<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/14/itinerary2-graph.jpg" style="width: 222px; height: 230px;" />
<pre>
<strong>输入:</strong>tickets = [["JFK","SFO"],["JFK","ATL"],["SFO","ATL"],["ATL","JFK"],["ATL","SFO"]]
<strong>输出:</strong>["JFK","ATL","JFK","SFO","ATL","SFO"]
<strong>解释:</strong>另一种有效的行程是 ["JFK","SFO","ATL","JFK","ATL","SFO"] ,但是它字典排序更大更靠后。
</pre>

<p> </p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 <= tickets.length <= 300</code></li>
	<li><code>tickets[i].length == 2</code></li>
	<li><code>from<sub>i</sub>.length == 3</code></li>
	<li><code>to<sub>i</sub>.length == 3</code></li>
	<li><code>from<sub>i</sub></code><code>to<sub>i</sub></code> 由大写英文字母组成</li>
	<li><code>from<sub>i</sub> != to<sub>i</sub></code></li>
</ul>

<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
44

每日一练社区's avatar
每日一练社区 已提交
45
## aop
F
fix bug  
feilong 已提交
46

每日一练社区's avatar
每日一练社区 已提交
47
### before
F
fix bug  
feilong 已提交
48

每日一练社区's avatar
每日一练社区 已提交
49 50 51 52 53
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
54

每日一练社区's avatar
每日一练社区 已提交
55 56 57 58 59
```cpp

```

## 答案
F
fix bug  
feilong 已提交
60

每日一练社区's avatar
每日一练社区 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
```cpp
unordered_map<int, string> rev;
int cmp(pair<int, int> x, pair<int, int> y)
{
    return rev[x.first] < rev[y.first];
}
class Solution
{
public:
    unordered_map<string, int> mp;
    int cnt = 0;
    int head[10005], nex[10005], to[10005], tot = 0;
    int vis[10005];
    vector<string> ans;
    void add(int x, int y)
    {
        to[++tot] = y;
        nex[tot] = head[x];
        head[x] = tot;
    }

    void euler(int x)
    {
        vector<pair<int, int>> vec;
        for (int i = head[x]; i; i = nex[i])
        {
            int v = to[i];
            if (vis[i])
            {
                continue;
            }
            vec.push_back({v, i});
        }
        sort(vec.begin(), vec.end(), cmp);
        for (int i = 0; i < vec.size(); i++)
        {
            if (vis[vec[i].second])
                continue;
            vis[vec[i].second] = 1;
            euler(vec[i].first);
            ans.push_back(rev[vec[i].first]);
        }
    }
    vector<string> findItinerary(vector<vector<string>> &tickets)
    {
        mp["JFK"] = 1;
        rev[1] = "JFK";
        cnt = 1;
        for (int i = 0; i < tickets.size(); i++)
        {
            string s1 = tickets[i][0], s2 = tickets[i][1];
            if (mp[s1])
            {
                mp[s1] = ++cnt;
                rev[cnt] = s1;
            }
            if (mp[s2])
            {
                mp[s2] = ++cnt;
                rev[cnt] = s2;
            }
            add(mp[s1], mp[s2]);
        }
        euler(1);
        ans.push_back(rev[1]);
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

```
## 选项

F
fix bug  
feilong 已提交
134

每日一练社区's avatar
每日一练社区 已提交
135
### A
F
fix bug  
feilong 已提交
136

每日一练社区's avatar
每日一练社区 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
```cpp
class Solution
{

public:
    vector<string> ans;
    unordered_map<string, vector<string>> ticket;
    unordered_map<string, vector<int>> use;
    bool dfs(string &now, int begin, int n)
    {
        if (begin == n)
        {
            return true;
        }
        else
        {
            int size = ticket[now].size();
            for (int i = 0; i < size; i++)
            {
                if (!use[now][i])
                {
                    ans.push_back(ticket[now][i]);
                    use[now][i] = 1;
                    if (dfs(ticket[now][i], begin + 1, n))
                        return true;
                    ans.pop_back();
                    use[now][i] = 0;
                }
            }
        }
        return false;
    }
    vector<string> findItinerary(vector<vector<string>> &tickets)
    {
        int n = tickets.size();
        for (int i = 0; i < n; i++)
        {
            int j, n = ticket[tickets[i][0]].size();
            for (j = 0; j < n; j++)
                if (ticket[tickets[i][0]][j] >= tickets[i][1])
                    break;
            ticket[tickets[i][0]].insert(ticket[tickets[i][0]].begin() + j, tickets[i][1]);
            use[tickets[i][0]].push_back(0);
        }
        string beginC = "JFK";
        ans.push_back(beginC);
        dfs(beginC, 0, n);
        return ans;
    }
};
```

### B
F
fix bug  
feilong 已提交
190

每日一练社区's avatar
每日一练社区 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
```cpp
class Solution
{
    unordered_map<string, multiset<string>> m;
    vector<string> ans;

public:
    vector<string> findItinerary(vector<vector<string>> &tickets)
    {
        for (auto &t : tickets)
            m[t[0]].insert(t[1]);
        dfs("JFK");
        reverse(ans.begin(), ans.end());
        return ans;
    }
    void dfs(string s)
    {
        while (m[s].size() != 0)
        {
            string to = *m[s].begin();
            m[s].erase(m[s].begin());
            dfs(to);
        }
        ans.push_back(s);
    }
};
```

### C
F
fix bug  
feilong 已提交
220

每日一练社区's avatar
每日一练社区 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
```cpp
class Solution
{
public:
    struct cmp
    {
        bool operator()(const string &a, const string &b)
        {
            return a > b;
        }
    };

    vector<string> findItinerary(vector<pair<string, string>> tickets)
    {
        map<string, int> m;
        vector<priority_queue<string, vector<string>, cmp>> queues;
        for (auto p : tickets)
        {
            if (m.find(p.first) == m.end())
            {
                priority_queue<string, vector<string>, cmp> newQueue;
                newQueue.push(p.second);
                queues.push_back(newQueue);
                m.insert(make_pair(p.first, queues.size() - 1));
            }
            else
            {
                queues[m[p.first]].push(p.second);
            }
        }

        vector<string> ans;
        stack<string> visitedPlaces;
        visitedPlaces.push("JFK");

        while (!visitedPlaces.empty())
        {
            string current = visitedPlaces.top();
            if (m.find(current) == m.end() || queues[m[current]].size() == 0)
            {
                ans.push_back(current);
                visitedPlaces.pop();
            }
            else
            {
                visitedPlaces.push(queues[m[current]].top());
                queues[m[current]].pop();
            }
        }

        reverse(ans.begin(), ans.end());
        return ans;
    }
};
```