holiday/8.15/F-Candies/F-Candies.cpp
2022-08-15 21:14:35 +08:00

71 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
const int N = 3e4 + 5, N3 = 15e4 + 5, inf = 0x3f3f3f3f;
int to[N3], ne[N3], w[N3], h[N], cnt = 0, s = N - 4, e, dist[N];
bool in[N], wa = 0;
int u, v, w1;//WA 和bool放一起了
int n, m, maxn = 0;
void add(int u, int v, int w1)
{
to[++cnt] = v;
ne[cnt] = h[u];
w[cnt] = w1;
h[u] = cnt;
}
void spfa(int s)
{
memset(dist,0x3f,sizeof dist);//更好的賦初值方法
dist[s] = 0;//和賦inf顺序反了=白干
stack<int> q; // queue没有自带函数清零
q.push(s);
while (!q.empty())
{
u = q.top();
q.pop();
in[u] = 0; // spfa的精髓
for (int i = h[u]; i; i = ne[i])
{
v = to[i], w1 = w[i];
// printf("%d %d %d\n",u,v,w1);
if (dist[u] + w1 < dist[v])
{
dist[v] = dist[u] + w1;
// printf("%d(%d) %d(%d) %d\n",u,dist[u],v,dist[v],w1);
if (!in[v])
{
q.push(v);
in[v] = 1;
}
}
}
}
}
int main()
{
scanf("%d%d", &n, &m);
// for (int i = 0; i <= n; i++)
// {
// dist[i] = inf;
// }
for (int i = 1; i <= m; i++) // n,m不分
{
scanf("%d%d%d", &u, &v, &w1);
add(u, v, w1);
}
spfa(1);
// maxn = 0;
// for(int i=2;i<=n;i++)
// {
// if(dist[i]>maxn)
// {
// maxn=dist[i];
// }
// }
printf("%d", dist[n]);
return 0;
}