71 lines
1.3 KiB
C++
71 lines
1.3 KiB
C++
|
#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 <20><>bool<6F><6C>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
|||
|
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);//<2F><><EFBFBD>õ<EFBFBD><C3B5>x<EFBFBD><78>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>
|
|||
|
dist[s] = 0;//<2F><><EFBFBD>xinf˳<66><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>=<3D><EFBFBD>
|
|||
|
stack<int> q; // queueû<65><C3BB><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
q.push(s);
|
|||
|
while (!q.empty())
|
|||
|
{
|
|||
|
u = q.top();
|
|||
|
q.pop();
|
|||
|
in[u] = 0; // spfa<66>ľ<EFBFBD><C4BE>裡<EFBFBD><E8A3A1>
|
|||
|
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<><6D><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|