最短路+输出路径+额外信息+映射

题目详情 - L3-1 直捣黄龙 (30 分) (pintia.cn)


#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>

using namespace std;

typedef pair<int, int> PII;

const int N = 300, M = 1e5 + 10, INF = 0x3f3f3f3f;

int n, m, hh, tt, id;
int h[N], e[M], ne[M], w[M], idx;
int enemy[N], kill[N], city[N], path[N];
int dist[N], pre[N];
bool st[N];
map<int, string> its;   //输出的时候将编号转换为字符串
map<string ,int> sti;   //输入的时候将字符串转换为编号

void add(int a, int b, int c)
{
    w[idx] = c;
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void djikstra(int u)
{
    memset(st, false, sizeof st);
    memset(dist, 0x3f, sizeof dist);
    
    priority_queue<PII, vector<PII>, greater<PII> > q;
    dist[u] = 0; 
    city[u] = 1; kill[u] = enemy[u]; path[u] = 1; 
    
    q.push({dist[u], u});
    while(q.size())
    {
        auto t = q.top();   q.pop();
        int ver = t.second, distance = t.first;
        if(st[ver]) continue;
        st[ver] = true;
        for(int i = h[ver]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > distance + w[i])
            {
                dist[j] = distance + w[i];
                pre[j] = ver;
                q.push({dist[j], j});
                
                city[j] = city[ver] + 1;
                kill[j] = kill[ver] + enemy[j];
                path[j] = path[ver];
            }
            else if(dist[j] == distance + w[i])
            {
                path[j] += path[ver];//坑点,当距离相同时,路径变多
                if(city[j] < city[ver] + 1)
                {
                    pre[j] = ver;
                    
                    city[j] = city[ver] + 1;
                    kill[j] = kill[ver] + enemy[j];
                }
                else if(city[j] == city[ver] + 1)
                {
                    if(kill[j] < kill[ver] + enemy[j])
                    {
                        pre[j] = ver;
                        kill[j] = kill[ver] + enemy[j];
                    }
                }
            }
        }
    }
    
}

void out(int u)
{
    if(pre[u] == -1)
    {
        cout << its[u];
        return ;
    }
    out(pre[u]);
    cout << "->" << its[u];
}

int main()
{
    string s, ss;
    int v;
    
    memset(pre, -1, sizeof pre);
    memset(h, -1, sizeof h);
    
    cin >> n >> m >> s >> ss;
    
    sti[s] = id;   its[id] = s; hh = id ++ ;
    sti[ss] = id;  its[id] = ss; tt = id ++ ;
    for(int i = 0; i < n - 1; i ++ )
    {
        cin >> s >> v;
        if(!sti.count(s))
        {
            sti[s] = id;
            its[id ++ ] = s;   
        }
        enemy[sti[s]] = v;
    }
    while(m -- )
    {
        cin >> s >> ss >> v;
        if(!sti.count(s))
        {
            sti[s] = id;
            its[id ++ ] = s;   
        }
        if(!sti.count(ss))
        {
            sti[ss] = id;
            its[id ++ ] = s;   
        }
        add(sti[s], sti[ss], v);
        add(sti[ss], sti[s], v);
    }
    
    djikstra(hh);
    
    out(tt);
    cout << endl << path[tt] << " " << dist[tt] << " " << kill[tt] << endl;
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值