#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;
}
给出每个站点之间的最短距离,求出最短路径,用unordered_map来实现,让你实现find_cheapest_transform函数
最新推荐文章于 2018-04-03 09:21:49 发布