[PTA] 直捣黄龙

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int > PII;
const int N=210;
const int INF=0x3f3f3f3f;

int n,m;
unordered_map<int,string> mpIS; 
unordered_map<string,int> mpSI; 
vector<vector<PII>> nodes(N);
int st,ed;
string sSt,sEd;
int nums[N];


int dis[N];
int killed[N];
int pre[N];
int cnt[N];
bool vis[N];
int path[N];


void dij(){
    priority_queue<PII,vector<PII>,greater<PII>>q;
    memset(dis,INF,sizeof dis);
    memset(killed,0,sizeof killed);
    memset(cnt,0,sizeof cnt);
    memset(vis,false,sizeof vis);
    memset(path,0,sizeof path);
    dis[st]=0;
    cnt[st]=0;
    path[st]=1;
    q.push({0,st});
    while(!q.empty()){
        PII temp=q.top();
        q.pop();
        int t=temp.second;
        if(vis[t]){
            continue;
        }
        vis[t]=true;
        for(PII el:nodes[t]){
            int v=el.first;
            int w=el.second;
            if(dis[v]>dis[t]+w){
                dis[v]=dis[t]+w;
                cnt[v]=cnt[t]+1;
                path[v]=path[t];
                pre[v]=t;
                killed[v]=killed[t]+nums[v];
                q.push({dis[v],v});
            }
            else if(dis[v]==dis[t]+w){
                path[v]+=path[t];
                if(cnt[t]+1>cnt[v]){
                    cnt[v]=cnt[t]+1;
                    pre[v]=t;
                    killed[v]=killed[t]+nums[v];
                }
                else if(cnt[t]+1==cnt[v] && killed[v]<killed[t]+nums[v]){
                    pre[v]=t;
                    killed[v]=killed[t]+nums[v];
                }
            }
        }
    }
    stack<int>stk;
    for(int i=ed;i!=st;i=pre[i]){
        stk.push(i);
    }
    // stk.push(st);
    
    printf("%s",mpIS[st].c_str());
    while(!stk.empty()){
        printf("->%s",mpIS[stk.top()].c_str());
        stk.pop();
    }
    printf("\n");
    printf("%d %d %d\n",path[ed],dis[ed],killed[ed]);
}
int main(){
    cin>>n>>m>>sSt>>sEd;
    mpIS[1]=sSt;
    mpSI[sSt]=1;
    for(int i=2;i<=n;i++){
        string temp;
        int tempNum;
        cin>>temp>>tempNum;
        mpSI[temp]=i;
        mpIS[i]=temp;
        nums[i]=tempNum;
    }
    for(int i=0;i<m;i++){
        string sa,sb;
        int a,b,c;
        cin>>sa>>sb>>c;
        a=mpSI[sa];
        b=mpSI[sb];
        nodes[a].push_back({b,c});
        nodes[b].push_back({a,c});
    }

    st=1;
    ed=mpSI[sEd];
    dij();

    return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值