我决定保留原来的代码发表更加精简的一个dijkstra。现在没时间,等到毕业去好好的更新博客吧。
#include<cstdio>
#include<iostream>
using namespace std;
#include<queue>
#include<cstring>
#define maxn 2600
#define inf 0x3f3f3f3f
#define in(x) scanf("%d",&x)
struct edge{
int to;
int dis;
int next;
}edges[maxn*6];
struct headnode{
int u;
int d;
bool operator <(const headnode& rhs)const{
return d>rhs.d;
}
};
int head[maxn];
int num;
void add_edge(int from,int to,int dis){
num++;
edges[num].to=to;
edges[num].dis=dis;
edges[num].next=head[from];
head[from]=num;
}
int n,m;
int s,t;
int d[maxn];
int p[maxn];
int vis[maxn];
void djstra(int s){
priority_queue<headnode>q;
memset(d,inf,sizeof(d));
d[s]=0;
q.push((headnode){s,0});
while(!q.empty()){
int u=q.top().u;
q.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=head[u];i!=0;i=edges[i].next){
int v=edges[i].to;
if(d[u]+edges[i].dis<d[v]){
d[v]=d[u]+edges[i].dis;
p[v]=u;
q.push((headnode){v,d[v]});
}
}
}
}
int main(){
in(n);
in(m);
in(s);
in(t);
for(int i=1;i<=m;i++)
{int a,b,c;
in(a);
in(b);
in(c);
add_edge(a,b,c);
add_edge(b,a,c);
}
djstra(s);
cout<<d[t];
}