一、单源最短路
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500010;
typedef pair<int, int> PII;
#define x first
#define y second
int n, m;
bool st[N];
int dist[N];
int h[N], e[N], w[N], ne[N], idx;
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, 1});
while (!q.empty())
{
auto t = q.top();
q.pop();
int id = t.y, d = t.x;
if (st[id] == true)
continue;
st[id] = true;
for (int i = h[id]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > d + w[i])
{
dist[j] = d + w[i];
q.push({dist[j], j});
}
}
}
if (dist[n] == 0x3f3f3f3f)
return -1;
return dist[n];
}
int main()
{
int a, b, c;
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int res = dijstra();
printf("%d\n", res);
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500010;
int n, m, k;
bool st[N];
int dist[N];
int copys[N];
struct Edge
{
int a, b, w;
}edge[N];
int bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for (int i = 1; i <= k; i ++ )
{
memcpy(copys, dist, sizeof dist);
for (int j = 1; j <= m; j ++ )
{
int a = edge[j].a;
int b = edge[j].b;
int w = edge[j].w;
dist[b] = min(dist[b], copys[a] + w);
}
}
if (dist[n] > 0x3f3f3f3f / 2)
return -1;
return dist[n];
}
int main()
{
int a, b, w;
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= m; i ++ )
{
scanf("%d%d%d", &a, &b, &w);
edge[i] = {a, b, w};
}
int res = bellman_ford();
if ((res == -1) && (dist[n] != -1))
puts("impossible");
else
printf("%d\n", res);
return 0;
}
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500010;
int n, m;
bool st[N];
int dist[N];
int h[N], e[N], w[N], ne[N], idx;
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
int spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
q.push(1);
st[1] = true;
while (!q.empty())
{
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
if (dist[n] == 0x3f3f3f3f)
return -1;
return dist[n];
}
int main()
{
int a, b, c;
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int res = spfa();
if ((res == -1) && (dist[n] != -1))
puts("impossible");
else
printf("%d\n", res);
return 0;
}
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 500010;
int n, m;
bool st[N];
int cnt[N];
int dist[N];
int h[N], e[N], w[N], ne[N], idx;
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++;
}
bool spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
for (int i = 1; i <= n; i ++ )
{
q.push(i);
st[i] = true;
}
while (!q.empty())
{
int t = q.front();
q.pop();
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
cnt[j] = cnt[t] + 1;
dist[j] = dist[t] + w[i];
if (!st[j])
{
q.push(j);
st[j] = true;
}
if (cnt[j] >= n)
return true;
}
}
}
return false;
}
int main()
{
int a, b, c;
scanf("%d%d", &n, &m);
memset(h, -1, sizeof h);
while (m -- )
{
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
if (spfa())
puts("Yes");
else
puts("No");
return 0;
}
二、多元汇最短路
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 510, INF = 1e9;
int n, m, k;
int d[N][N];
void floyd()
{
for (int k = 1; k <= n; k ++ )
{
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++ )
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
/**初始化**/
for (int i = 1; i <= n; i ++ )
{
for (int j = 1; j <= n; j ++ )
{
if (i == j)
d[i][j] = 0;
else
d[i][j] = INF;
}
}
while (m -- )
{
int a, b, w;
scanf("%d%d%d", &a, &b, &w);
d[a][b] = min(d[a][b], w);
}
floyd();
while (k -- )
{
int x, y;
scanf("%d%d", &x, &y);
if (d[x][y] > INF / 2)
puts("impossible");
else
printf("%d\n", d[x][y]);
}
return 0;
}