-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1028.cpp
82 lines (82 loc) · 1.82 KB
/
1028.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
using namespace std;
// ³£¹æµÄËã·¨
void Dijkstra(int s, int G[501][501], int n, int* d);
int main()
{
int m;
cin >> m;
int graph[501][501];
for (int i = 0;i < m;i++)
{
int n, E, s, t;
cin >> n >> E >> s >> t;
int d[501] = {};
for (int j = 1;j <= n;j++)
{
d[j] = INT16_MAX;
for (int k = 1;k <= n;k++)
{
if (j == k)
{
graph[j][k] = 0;
}
else
{
graph[j][k] = INT16_MAX;
}
}
}
for (int j = 0;j < E;j++)
{
int u, v;
cin >> u >> v;
int temp;
cin >> temp;
if (graph[u][v] > temp)
{
graph[u][v] = temp;
graph[v][u] = graph[u][v];
}
}
Dijkstra(s, graph, n, d);
if (d[t] == INT16_MAX)
{
cout << -1 << endl;
}
else
{
cout << d[t] << endl;
}
}
}
void Dijkstra(int s, int G[501][501], int n, int* d)
{
d[s] = 0;
bool vis[501] = {};
for (int i = 0;i < n;i++)
{
int u = -1;
int MIN = INT16_MAX;
for (int j = 1;j <= n;j++)
{
if (vis[j] == false && d[j] < MIN)
{
u = j;
MIN = d[j];
}
}
if (u == -1)
{
return;
}
vis[u] = true;
for (int v = 1;v <= n;v++)
{
if (vis[v] == false && G[u][v] != INT16_MAX && d[u] + G[u][v] < d[v])
{
d[v] = d[u] + G[u][v];
}
}
}
}