what

#include<iostream>
#include<stack>
#include<map> 
#include<limits.h>
using namespace std;
map <string,int> m;
map<int,string>rm;
stack<int>sta;
int P;
int path[40][40];
int d[40][40];
void printpath(int p,int q)
{
while(path[p][q]!=p)
{
sta.push(path[p][q]);
q=path[p][q];
}
while(!sta.empty())
{
cout<<rm[sta.top()]<<" ";
sta.pop();
}
cout<<endl;
}
void f(int p,int q)
{
for(int i=0;i<P;i++)
{
for(int j=0;j<P;j++)
{
if(d[i][j]==INT_MAX)
path[i][j]=-1;
else
path[i][j]=i;
}
}
for(int i=0;i<P;i++)
{
for(int j=0;j<P;j++)
{
for(int k=0;k<P;k++)
{
if(d[i][k]+d[k][j]<d[i][j])
{
d[i][j]=d[i][k]+d[k][j];
path[i][k]=i;
path[i][j]=path[k][j];
}
}
}
}
printpath(p,q);
}
int main()
{
cin>>P;
for(int i=1;i<=P;i++)
{
string a;
cin>>a;
m.insert(pair<string,int>(a,i));
rm.insert(pair<int,string>(i,a));
}
int Q;
cin>>Q;
for(int i=1;i<=P;i++)
{
for(int j=1;j<=P;j++)
{
if(i==j)d[i][j]=0;
else d[i][j]=INT_MAX;
}
}
for(int i=1;i<=Q;i++)
{
string b,c;
int e;
cin>>b>>c>>e;
int f=m[b];
int g=m[c];
d[f][g]=e;
}
int R;
cin>>R;
for(int i=1;i<=R;i++)
{
string h,k;
cin>>h>>k;
int p=m[h];
int q=m[k];
f(p,q);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值