堆优化了一下
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
struct edge{
int to,cost;
};
int const N=10010;
typedef pair<int,int> P;//x是距离,y是编号
vector<edge> a[N];
priority_queue<P,vector<P>,greater<P> > q;
int n,dis[N];
int get(char a){
return int(a);
}
void dijk(){
memset(dis,0x3f,sizeof dis);
dis[get('Z')]=0;
q.push({0,get('Z')});
while(!q.empty()){
auto e=q.top();q.pop();
int from=e.y;//当前点
if(dis[from]<e.x)continue;
for(int i=0;i<a[from].size();i++){
auto e=a[from][i];//下一个点
if(dis[e.to]>dis[from]+e.cost){
dis[e.to]=dis[from]+e.cost;
q.push({dis[e.to],e.to});
}
}
}
}
int main(){
//ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=0;i<n;i++){
char from,to;
int w;cin>>from>>to>>w;
int u=get(from),v=get(to);
//printf("%d %d %d\n",u,v,w);
a[u].push_back({v,w});
a[v].push_back({u,w});
}
dijk();
int ans1=0x3f3f3f3f,ans2;
for(int i='A';i<='Z';i++)
//cout<<char(i)<<" "<<dis[i]<<endl;
for(int i='A';i<int('Z');i++){
if(ans1>dis[i]){
ans1=dis[i];
ans2=i;
}
}
cout<<char(ans2)<<" "<<ans1<<endl;
return 0;
}
//注意数据范围!小心爆int
//注意边界问题
//注意特殊情况!!!!!