解题思路:
老模版超时了,用的bin神用栈实现的SPFA。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define LL long long
using namespace std;
const int MAXN = 30000 + 10;
const int MAXE = 150000 + 10;
const int INF = 0x3f3f3f3f;
int head[MAXN];
int vis[MAXN];
int Q[MAXN];
int dis[MAXN];
struct Edge
{
int to;
int w;
int next;
}edge[MAXE];
int tot;
void add(int u, int v, int w)
{
edge[tot].to = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
void SPFA(int s, int n)
{
int top = 0;
for(int i=1;i<=n;i++)
{
if(i == s)
{
Q[top++] = s;
vis[i] = true;
dis[i] = 0;
}
else
{
vis[i] = false;
dis[i] = INF;
}
}
while(top != 0)
{
int u = Q[--top];
vis[u] = false;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].w)
{
dis[v] = dis[u] + edge[i].w;
if(!vis[v])
{
vis[v] = true;
Q[top++] = v;
}
}
}
}
}
int main()
{
int n, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(head, -1, sizeof(head));
tot = 0;
int u, v, w;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d", &u, &v, &w);
add(u,v,w);
}
SPFA(1,n);
printf("%d\n", dis[n]);
}
return 0;
}