给出每个站点之间的最短距离,求出最短路径,用unordered_map来实现,让你实现find_cheapest_transform函数
来源:互联网 发布:windows如何禁止更新 编辑:程序博客网 时间:2024/06/06 12:59
#include <map>#include <set>#include <list>#include <cmath>#include <ctime>#include <deque>#include <queue>#include <stack>#include <string>#include <bitset>#include <cstdio>#include <limits>#include <vector>#include <climits>#include <cstring>#include <cstdlib>#include <fstream>#include <numeric>#include <sstream>#include <iostream>#include <algorithm>#include <unordered_map>using namespace std;/** Complete the function below.*/int find_cheapest_transform(const string &input, const string &output, unordered_map<char, unordered_map<char, int> > cost_function){ int sum = 0; int len = input.size(); int distance[len][len]; auto row = cost_function.begin(); for(int i = 0;i < len;i++,row++) { auto col = row->second.begin(); for(int j = 0;j < len;j++,col++) { distance[i][j] = col->second; cout<<distance[i][j]<<" "; } cout<<endl; } for(int i = 0;i < len;i++) { for(int j = 0;j < len;j++) { for(int k = 0;k < len;k++) { if(distance[j][i]+distance[i][k] < distance[j][k]) distance[j][k] = distance[j][i]+distance[i][k]; } } } row = cost_function.begin(); for(int i = 0;i < len;i++,row++) { auto col = row->second.begin(); for(int j = 0;j < len;j++,col++) { col->second = distance[i][j]; } } for (int i = 0; i < len; i++) { char s = input[i]; char d = output[i]; sum += cost_function.at(s).at(d); } return sum;}int main() { int res; string _input = "abcd"; string _output = "bcda"; int rows = 4; int cols = 4; unordered_map<char, unordered_map<char, int> > map; unordered_map<char, int> s; s['a'] = 0; s['b'] = 2; s['c'] = 4; s['d'] = 5; map['a'] = s; unordered_map<char, int> t; t['a'] = 4; t['b'] = 0; t['c'] = 9; t['d'] = 6; map['b'] = t; unordered_map<char, int> m; m['a'] = 1; m['b'] = 4; m['c'] = 0; m['d'] = 2; map['c'] = m; unordered_map<char, int> n; n['a'] = 3; n['b'] = 2; n['c'] = 1; n['d'] = 0; map['d'] = n; char chars[4] = { 'a', 'b', 'c', 'd' }; res = find_cheapest_transform(_input, _output, map); cout << res << endl; return 0;}
0 0
- 给出每个站点之间的最短距离,求出最短路径,用unordered_map来实现,让你实现find_cheapest_transform函数
- 最短路径的实现
- 最短路径的实现
- 最短路径的实现
- 最短路径的实现
- 每对结点之间最短路径的C++实现
- 最短路径实现
- spark primer 计算每个每个顶点之间最短路径
- floyd最短路径算法的实现
- A*算法的最短路径实现!
- Dijkstra算法的最短路径实现
- dijkstra最短路径算法的实现
- Bellman-Ford 最短路径的实现
- 利用Dijkstra算法实现记录每个结点的所有最短路径
- 最短路径算法实现
- matlab实现最短路径
- 最短路径算法实现
- C#实现最短路径
- 顺序表---模板类实现
- 3-3使用生成器函数实现可迭代对象
- 【运营商】重庆有线破局之道
- 3-4进行反向迭代以及实现反向迭代
- c++ 容器vector的基本操作
- 给出每个站点之间的最短距离,求出最短路径,用unordered_map来实现,让你实现find_cheapest_transform函数
- Glide源码分析(一)——DiskLruCache磁盘缓存的实现
- 2013第四届蓝桥杯预赛试题本科c++前缀判断
- Sql profiles-->使用sql tuning生成sql profile
- LeetCode:535. Encode and Decode TinyURL
- iptables详解
- 3-5对迭代器做切片操作
- 带头结点的双向循环链表
- STl的set容器的总结