题目地址:https://vjudge.net/contest/66569#problem/A
思路:模板题
AC代码:
#include<iostream> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=1000+5; vector<pair<int,int> >E[maxn]; int t,n; int d[maxn]; int main() { scanf("%d%d",&t,&n); for(int i=0;i<t;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); E[a].push_back(make_pair(b,c)); E[b].push_back(make_pair(a,c)); } for(int i=0;i<n;i++) d[i]=1e9; int s=n,e=1; priority_queue<pair<int,int> >q; d[s]=0; q.push(make_pair(-d[s],s)); while(!q.empty()) { int now=q.top().second; q.pop(); for(int i=0;i<E[now].size();i++) { int v=E[now][i].first; if(d[v]>d[now]+E[now][i].second) { d[v]=d[now]+E[now][i].second; q.push(make_pair(-d[v],v)); } } } printf("%d\n",d[1]); }