L3-011 直捣黄龙

#include<bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;

const int N = 300;
vector<pair<int, int>> g[N];
int have[N];
int pre[N];
int path[N];
bool st[N];
int dist[N];
int n, k;
int Kill[N];
int jiefang[N];
string start, endd;
unordered_map<string, int> var;
unordered_map<int, string> local;

void dijkstra() {
   // memset(st, 0, sizeof st);
    memset(dist, 0x3f, sizeof dist);
    dist[var[start]] = 0;
    priority_queue<pii, vector<pii>, greater<pii>> heap;
    heap.push({dist[var[start]], var[start]});
    path[var[start]] = 1;
    while (heap.size()) {
        auto t = heap.top();
        heap.pop();
        int u = t.second;
        //cout << "u == " << u << " " << local[u] << "\n";;
        if (st[u]) continue;
        st[u] = true;
        for (auto itr : g[u]) {
            int v = itr.first, w = itr.second;
           // cout << "v" << " = " << v << " " << local[v] << "\n";
            //if (local[v] == "DBY") cout << "kaishi = " << "Kill " << Kill[v] << " jiefang " << jiefang[v] << "\n";
            if (dist[v] > dist[u] + w) { 
                pre[v] = u;
                Kill[v] = Kill[u] + have[v];
                jiefang[v] = jiefang[u] + 1;
                dist[v] = dist[u] + w;
                heap.push({dist[v], v});
               // cout << "---新更" << local[v] << " " << local[u] << "\n";
                path[v] = path[u];
               // cout << path[v] << " " << path[u] << "\n";
            } else if (dist[v] == dist[u] + w) {
                if (jiefang[v] < jiefang[u] + 1) {
                    pre[v] = u;
                    jiefang[v] = jiefang[u] + 1;
                    Kill[v] = Kill[u] + have[v];
                } else if (jiefang[v] == jiefang[u] + 1) {
                    if (Kill[v] < Kill[u] + have[v]) {
                        pre[v] = u;
                        Kill[v] = Kill[u] + have[v];
                    }
                }
                // cout << "---" << local[v] << " " << local[u] << "\n";
                // cout << path[v] << " " << path[u] << "\n";
                path[v] += path[u];
            }
            //if (local[v] == "DBY") cout << "jieshu = " << "Kill " << Kill[v] << " jiefang " << jiefang[v] << "\n";
            //cout << "pre " << local[v] << " = " << local[pre[v]] << "\n";
        }
        //cout << "\n\n";
    }
    
    int ed = var[endd];
    int edd = ed;
    int sta = var[start];
    //cout << local[ed] << " " << local[st] << "\n";
    vector<string> res; 
    while (ed != sta) {
        res.push_back(local[ed]);
        //cout << local[ed] << "\n";
        ed = pre[ed];
    }
    res.push_back(local[sta]);
    for (int i = res.size() - 1; i >= 0; i--) {
        cout << res[i];
        if (i != 0) cout << "->";
    }
    cout << "\n";
    //cout << local[ed] << "\n";
    //cout << 
    // cout << path[var["ATP"]] << "\n";
    // cout << path[var["PTA"]] << "\n";
    // cout << path[var["PMS"]] << "\n";
    // cout << path[var["LNN"]] << "\n";
    // cout << path[var["PDS"]] << "\n";
    // cout << path[var["TAP"]] << "\n";
    // cout << path[var["DBY"]] << "\n";
    cout << path[edd] << " ";
    cout << dist[edd] << " "; //距离正确s
    cout << Kill[edd] << "\n";
}

int main() {
    //map<int, int> have;
    cin >> n >> k;
    cin >> start >> endd;
    int id = 1;
    var[start] = id, local[id] = start, id++;
    var[endd] = id, local[id] = endd, id++;
    for (int i = 1; i <= n - 1; i++) {
        string s;
        int cnt;
        cin >> s >> cnt;
        if (var.count(s)) have[var[s]] = cnt;
        else {
            var[s] = id;
            local[id] = s;
            have[id] = cnt;
            id++;
        }
    }

    for (int i = 0; i < k; i++) {
        string a, b;
        int w;
        cin >> a >> b >> w;
        int vara = var[a], varb = var[b];
        g[vara].push_back({varb, w}); //a -> b w
        g[varb].push_back({vara, w});
    }

    dijkstra();
    
    return 0;
}
posted @ 2022-04-21 10:22  Xxaj5  阅读(28)  评论(0编辑  收藏  举报