spfa的vector存图
作者:
大瓜娃子
,
2022-01-27 12:10:17
,
所有人可见
,
阅读 269
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m;
int dist[N];
bool st[N];
vector<pair<int,int>> g[N];
void add(int a, int b, int c)
{
g[a].push_back({b,c});
}
int spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
q.push(1);
st[1] = true;
while (q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for (int i = 0; i<g[t].size(); i++)
{
int j = g[t][i].first;
if (dist[j] > dist[t] + g[t][i].second)
{
dist[j] = dist[t] + g[t][i].second;
if (!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return dist[n];
}
int main()
{
scanf("%d%d", &n, &m);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int t = spfa();
if (t == 0x3f3f3f3f) puts("impossible");
else printf("%d\n", t);
return 0;
}
👍👍👍