You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
56 lines
1.2 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
/*
|
|
(朴素dijkstra) O(n2)
|
|
*/
|
|
|
|
const int N = 2510;
|
|
const int M = 6200 * 2 + 10;
|
|
|
|
int n; // n个城镇
|
|
int m; // m条路径
|
|
int S; //起点
|
|
int T; //终点
|
|
//邻接表
|
|
int h[N], w[M], e[M], ne[M], idx;
|
|
bool st[N]; //是否使用过
|
|
int dist[N]; //最短距离数组
|
|
|
|
//维护邻接表
|
|
void add(int a, int b, int c) {
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
}
|
|
|
|
int dijkstra() {
|
|
memset(dist, 0x3f, sizeof dist); //初始化为无穷大
|
|
dist[S] = 0; //出发点的距离初始化为0
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
//找到距离出发点最近的点
|
|
int t = -1;
|
|
for (int j = 1; j <= n; j++)
|
|
if (!st[j] && (t == -1 || dist[j] < dist[t]))
|
|
t = j;
|
|
|
|
st[t] = true;
|
|
for (int j = h[t]; ~j; j = ne[j]) {
|
|
int k = e[j];
|
|
dist[k] = min(dist[k], dist[t] + w[j]);
|
|
}
|
|
}
|
|
return dist[T];
|
|
}
|
|
|
|
int main() {
|
|
cin >> n >> m >> S >> T;
|
|
memset(h, -1, sizeof h);
|
|
for (int i = 1; i <= m; i++) {
|
|
int a, b, c;
|
|
cin >> a >> b >> c;
|
|
add(a, b, c), add(b, a, c);
|
|
}
|
|
cout << dijkstra() << endl;
|
|
return 0;
|
|
}
|