自己整理的模板,仅作模板保存使用
#include<iostream>
#include<queue>
#include<algorithm>
#include<string.h>
#define ll long long
#define pa pair<int,int>
using namespace std;
const int maxn=1005;
const int maxe=2*2010;
const int inf=1e8;
int step,n,m;
int head[maxn];
int dis[maxn];
struct Ed
{
int to,next;
int val;
}edge[maxe];
void add(int x,int y,int z)
{
edge[step].to=y;
edge[step].val=z;
edge[step].next=head[x];
head[x]=step;
step++;
}
priority_queue<pa,vector<pa>,greater<pa> >Q;
void dijkstar(int s)
{
for(int i=0;i<=n;i++) dis[i]=inf;
dis[s]=0;
Q.push(make_pair(0,s)); //first是距离,second是节点编号
while(!Q.empty()){
pa cur=Q.top();
Q.pop();
int now=cur.second;
if(dis[now]<cur.first) continue; //当前边比最小的边还小,不用更新
for(int i=head[now];i!=-1;i=edge[i].next){
if(dis[edge[i].to]>dis[now]+edge[i].val){
dis[edge[i].to]=dis[now]+edge[i].val;
Q.push(make_pair(dis[edge[i].to],edge[i].to));
}
}
}
}
int main()
{
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int t;
// cin>>t;
// while(t--){
cin>>m>>n;
step=1;
memset(head,-1,sizeof(head));
while(!Q.empty()) Q.pop();
for(int i=1;i<=m;i++){
int x,y;
int z;
cin>>x>>y>>z;
add(y,x,z); //在此题中可以走回头路
add(x,y,z);
}
dijkstar(n);
cout<<dis[1]<<endl;
// }
}