#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;
}
[PTA] 直捣黄龙
最新推荐文章于 2024-07-14 19:46:54 发布