给出每个站点之间的最短距离,求出最短路径,用unordered_map来实现,让你实现find_cheapest_transform函数

#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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值