7-14 直捣黄龙 (30分)

7-14 直捣黄龙 (30分)

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<iomanip>
#include<stack>
using namespace std;
#define STDIN freopen("in.in", "r", stdin);freopen("out.out", "w", stdout);


const int N = 205;
int G[N][N];

int n, k;
map<string, int > ma1;
map<int,string> ma2;
string src, des;
int cnt = 0;
int nums[N];
int dist[N];
bool st[N];
int step[N];
int shadi[N];
int pre[N];
int lujing[N];
vector<string> ans;
void rele(string a, int b)
{
    ma1[a] = b;
    ma2[b] = a;
}
int dijkstra(int str)
{
    memset(dist, 0x3f, sizeof dist);
    dist[str] = 0;
    step[str] = 0;
    shadi[str] = 0;
    lujing[str] = 1;
    for (int i = 0; i  < cnt - 1; i++)
    {
        int t = -1;
        for (int j = 1; j <= cnt; j++)
        {
            if (!st[j] && (t == -1 || dist[t] > dist[j])) t=j;
        }
        for (int j = 1; j <= cnt ;j++)
        {
            if (dist[j] > dist[t] + G[t][j])
            {
                dist[j] = dist[t] + G[t][j];
                step[j] = step[t] + 1;
                shadi[j] = shadi[t] + nums[j];
                pre[j] = t;
                lujing[j] = lujing[t];
            }
            if (dist[j] == dist[t] + G[t][j])
            {
                if (pre[j] != t)
                    lujing[j] += lujing[t];
                if (step[j] < step[t] + 1) {
                    step[j] = step[t] + 1;
                    shadi[j] = shadi[t] + nums[j];
                    pre[j] = t;
                }
                if (step[j] == step[t] + 1)
                {
                    if (shadi[j] < shadi[t] + nums[j])
                    {
                        shadi[j] = shadi[t] + nums[j];
                        pre[j] = t;
                    }
                }
            }
        }
        st[t] = true;
    }

    if (dist[ma1[des]] == 0x3f3f3f3f) return -1;
    return dist[ma1[des]];
}
void print(string u)
{
    if (ma1[u] != 0)
    {
        print(ma2[pre[ma1[u]]]);
        ans.push_back(u);
    }
}
int main()
{
//     STDIN
    cin >> n >> k;
    memset(G, 0x3f, sizeof G);
    cin >> src >> des;
    rele(src, ++cnt);
    rele(des, ++cnt);
    for (int i = 1; i < n; i++)
    {
        string t; int x;
        cin >> t >> x;
        if (ma1.count(t) == 0){
            rele(t, ++cnt);
        }
        nums[ma1[t]] = x;
    }
    
    for (int i = 1;  i<= k; i++)
    {
        string a, b;
        int len;
        cin >> a >> b >> len;
        int ia = ma1[a], ib = ma1[b];
        G[ia][ib] = min(G[ia][ib], len);
        G[ib][ia] = min(G[ib][ia], len);
    }
    dijkstra(ma1[src]);
    print(des);
    int len = ans.size();
    for (int i = 0;  i< len;i++)
    {
        cout << ans[i];
        if (i == len-1) cout << endl;
        else cout << "->";
    }
    cout << lujing[ma1[des]] << " " << dist[ma1[des]] << " " << shadi[ma1[des]] << endl;
    return 0;
}

 

posted @ 2020-11-27 19:31  hulian425  阅读(454)  评论(0编辑  收藏  举报