80 lines
1.5 KiB
C++
80 lines
1.5 KiB
C++
#include <iostream>
|
|
#include <cstring>
|
|
#include <algorithm>
|
|
#include <stack>
|
|
using namespace std;
|
|
|
|
typedef long long LL;
|
|
typedef pair<int, int> PII;
|
|
const int INF = 0x3f3f3f3f;
|
|
|
|
const int N = 30005, M = 150005;
|
|
|
|
inline int read()
|
|
{
|
|
register int x = 0, f = 1;
|
|
register char ch = getchar();
|
|
while(ch < '0' || ch > '9')
|
|
{
|
|
if(ch == '-') f = -1;
|
|
ch = getchar();
|
|
}
|
|
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
|
|
return x * f;
|
|
}
|
|
|
|
int n, m;
|
|
struct Edge
|
|
{
|
|
int v,nxt,w;
|
|
}e[M];
|
|
int head[N], idx = 0;
|
|
inline void add(int u,int v,int w=0)
|
|
{
|
|
e[++idx].v = v,e[idx].w = w, e[idx].nxt = head[u],head[u] = idx;
|
|
}
|
|
|
|
int dis[N], st[N], times[N];
|
|
bool spfa(int start)
|
|
{
|
|
stack<int> q;
|
|
memset(dis,0x3f,sizeof dis);
|
|
dis[start] = 0, st[start] = times[start] = 1, q.push(start);
|
|
|
|
while(!q.empty())
|
|
{
|
|
int u = q.top(); q.pop();
|
|
|
|
st[u] = 0;
|
|
for(int i=head[u];i;i=e[i].nxt)
|
|
{
|
|
int v = e[i].v, w = e[i].w;
|
|
if(dis[v] > dis[u] + w)
|
|
{
|
|
dis[v] = dis[u] + w;
|
|
if(!st[v])
|
|
{
|
|
st[v] = 1, q.push(v);
|
|
// if(times[v] >= n) return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
int main()
|
|
{
|
|
n = read(), m = read();
|
|
for(int i=1;i<=m;i++)
|
|
{
|
|
int u = read(),v = read(),w = read();
|
|
add(u,v,w);
|
|
}
|
|
|
|
spfa(1);
|
|
cout<<dis[n]<<endl;
|
|
|
|
return 0;
|
|
} |