日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode 1548. The Most Similar Path in a Graph(动态规划)

發布時間:2024/7/5 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode 1548. The Most Similar Path in a Graph(动态规划) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 1. 題目
    • 2. 解題

1. 題目

We have n cities and m bi-directional roads where roads[i] = [ai, bi] connects city ai with city bi. Each city has a name consisting of exactly 3 upper-case English letters given in the string array names. Starting at any city x, you can reach any city y where y != x (i.e. the cities and the roads are forming an undirected connected graph).

You will be given a string array targetPath. You should find a path in the graph of the same length and with the minimum edit distance to targetPath.

You need to return the order of the nodes in the path with the minimum edit distance, The path should be of the same length of targetPath and should be valid (i.e. there should be a direct road between ans[i] and ans[i + 1]). If there are multiple answers return any one of them.

The edit distance is defined as follows:

Follow-up: If each node can be visited only once in the path, What should you change in your solution?

Example 1:

Input: n = 5, roads = [[0,2],[0,3],[1,2],[1,3],[1,4],[2,4]], names = ["ATL","PEK","LAX","DXB","HND"], targetPath = ["ATL","DXB","HND","LAX"] Output: [0,2,4,2] Explanation: [0,2,4,2], [0,3,0,2] and [0,3,1,2] are accepted answers. [0,2,4,2] is equivalent to ["ATL","LAX","HND","LAX"] which has edit distance = 1 with targetPath. [0,3,0,2] is equivalent to ["ATL","DXB","ATL","LAX"] which has edit distance = 1 with targetPath. [0,3,1,2] is equivalent to ["ATL","DXB","PEK","LAX"] which has edit distance = 1 with targetPath.

Example 2:

Input: n = 4, roads = [[1,0],[2,0],[3,0],[2,1],[3,1],[3,2]], names = ["ATL","PEK","LAX","DXB"], targetPath = ["ABC","DEF","GHI","JKL","MNO","PQR","STU","VWX"] Output: [0,1,0,1,0,1,0,1] Explanation: Any path in this graph has edit distance = 8 with targetPath.

Example 3:

Input: n = 6, roads = [[0,1],[1,2],[2,3],[3,4],[4,5]], names = ["ATL","PEK","LAX","ATL","DXB","HND"], targetPath = ["ATL","DXB","HND","DXB","ATL","LAX","PEK"] Output: [3,4,5,4,3,2,1] Explanation: [3,4,5,4,3,2,1] is the only path with edit distance = 0 with targetPath. It's equivalent to ["ATL","DXB","HND","DXB","ATL","LAX","PEK"]Constraints: 2 <= n <= 100 m == roads.length n - 1 <= m <= (n * (n - 1) / 2) 0 <= ai, bi <= n - 1 ai != bi The graph is guaranteed to be connected and each pair of nodes may have at most one direct road. names.length == n names[i].length == 3 names[i] consists of upper-case English letters. 1 <= targetPath.length <= 100 targetPath[i].length == 3 targetPath[i] consists of upper-case English letters.

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/the-most-similar-path-in-a-graph
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

2. 解題

class Solution { public:vector<int> mostSimilar(int n, vector<vector<int>>& roads, vector<string>& names, vector<string>& targetPath) {vector<vector<int>> g(n);for(auto& r : roads){g[r[0]].push_back(r[1]);g[r[1]].push_back(r[0]);}//建圖int len = targetPath.size();vector<vector<int>> dp(len, vector<int>(n, INT_MAX));//走完 ?target后的 在城市 ni 的最小編輯距離vector<vector<int>> path1(n);//n個城市作為結束的最佳路線vector<vector<int>> path2(n);//存儲下一個狀態的路徑for(int i = 0; i < n; ++i)//初始化{dp[0][i] = (names[i] != targetPath[0]);path1[i].push_back(i);}int mindis = INT_MAX, minidx = -1;for(int k = 1; k < len; ++k){ //樣本維度for(int i = 0; i < n; ++i){ //前一個城市if(dp[k-1][i] == INT_MAX)continue;for(int j : g[i]){ //下一個相連的城市if(dp[k][j] > dp[k-1][i]+(names[j]!=targetPath[k])){dp[k][j] = dp[k-1][i]+(names[j]!=targetPath[k]);path2[j] = path1[i];path2[j].push_back(j);}}}swap(path1, path2);//滾動數組,更新當前的最佳路徑至path1}for(int i = 0; i < n; i++) {if(mindis > dp[len-1][i]){mindis = dp[len-1][i];minidx = i;}}//取編輯距離最小的城市編號return path1[minidx];//返回路徑} };

1260 ms 109.6 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的LeetCode 1548. The Most Similar Path in a Graph(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 午夜精品久久久久久久99 | 亚洲精品乱码久久久久久蜜桃图片 | 不卡二区 | 天天干天天爽天天操 | 夜夜久久久 | 欧美日韩高清一区二区 | 成年人在线网站 | 无码精品人妻一区二区三区影院 | 久久久国产一区 | 成人做受视频试看60秒 | 久久久看片| 久色亚洲 | 欧洲视频一区二区三区 | 成人毛片100部免费看 | 亚洲在线免费 | 精品人伦一区二区三区蜜桃免费 | 性生交大片免费看3p | 久久99精品国产麻豆91樱花 | 欧美老熟妇又粗又大 | 性高湖久久久久久久久aaaaa | 亚洲精品福利在线观看 | 91视频在线免费看 | 欧美日本免费 | 亚洲一区二区成人 | 久久久www成人免费毛片 | 91久久色| 动漫玉足吸乳羞免费网站玉足 | 国产91在线高潮白浆在线观看 | 吃奶摸下的激烈视频 | www.国产免费 | 久草国产视频 | 最新中文字幕久久 | 三年中文免费观看大全动漫 | 国产精品亚洲二区在线观看 | 欧美精品在线一区二区三区 | 久久免费成人 | 色涩视频在线观看 | 在线va| 国产高清免费在线播放 | 欧美你懂的 | 成人性生交大片免费看vrv66 | 欧洲亚洲成人 | 欧美精品一区二区免费 | 欧美裸体网站 | 午夜影院在线观看免费 | 欧美一级爆毛片 | 咪咪成人网 | 日韩视频免费观看高清完整版 | 亚洲乱码国产乱码精品天美传媒 | 最新视频 - 8mav | 久久亚洲热 | 在线视频一区二区三区 | 国产亚洲欧美一区二区三区 | 一级片一区 | 国产精品怡红院 | 宅男噜噜噜666在线观看 | 成人在线视频一区二区 | 超污巨黄的小短文 | 国产黄色特级片 | 精品一区av| 俺来也俺也啪www色 欧洲一区二区视频 | 日本免费看| 亚洲色婷婷久久精品av蜜桃 | 中文字幕久久av | 一区二区三区日韩精品 | 黄色成人在线免费观看 | 97精品国产97久久久久久春色 | 在线播放国产一区 | 日韩视频在线视频 | 欧美高清不卡 | 手机版av| 可以在线看黄的网站 | 76少妇精品导航 | 看免费黄色大片 | 国产www视频| 黄色网址www| 精品无码国产污污污在线观看 | 国产精品视频一二区 | www亚洲一区| 国产剧情av引诱维修工 | 一区二区三区在线免费观看视频 | 激情伊人五月天 | 一级h片 | 亚洲视频免费在线 | 一区二区三区视频免费看 | 狠狠搞av | 精品国产乱码久久久久久久 | 国产理论av | 亚洲视频在线观看一区 | 国产精品久久久免费 | 色综合天天综合网国产成人网 | 黄色一区二区视频 | 高清av网站 | 伊人影视大全 | 500福利视频导航 | 免费国产高清 | 91视频在线视频 | 天天操狠狠操夜夜操 | 久草最新视频 |