杭电1874——SPFA(最短路径)
原题传送门
迪杰斯特解题拉传送门
弗洛伊德传送门
裸题裸题!!!
# include <iostream>
# include <algorithm>
# include <queue>
# include <vector>
# include <stack>
# include <cstdio>
# include <cstring>
using namespace std;
const int maxn = 1e9;
const int sl = 205;
queue <int> que;
vector <int> ss[sl];//邻接表
int len[sl], vis[sl],mapp[sl][sl];
void reset() {//初始化、重置
memset(vis, 0, sizeof(vis));
for (int i = 0; i < sl; i++) {
for (int j = 0; j < sl; j++) {
if (i == j) {
mapp[i][j] = 0;
}
else {
mapp[i][j] = maxn;
}
}
ss[i].clear();
len[i] = maxn;
}
while (!que.empty()) {
que.pop();
}
}
int main(void) {
ios::sync_with_stdio(false);
int n, m;
int a, b, x;
int s, t;
while (cin >> n >> m) {
reset();
while (m--) {
cin >> a >> b >> x;
if (mapp[a][b] > x) {
mapp[a][b] = mapp[b][a] = x;
}
ss[a].push_back(b);
ss[b].push_back(a);
}
cin >> s >> t;
len[s] = 0;
que.push(s);
vis[s] = 1;
while (!que.empty()) {
int k = que.front();
for (int i = 0; i < ss[k].size(); i++) {
if (len[ss[k][i]] > len[k] + mapp[k][ss[k][i]]) {
len[ss[k][i]] = len[k] + mapp[k][ss[k][i]];
if (vis[ss[k][i]]==0) {
que.push(ss[k][i]);
vis[ss[k][i]] = 1;
}
}
}
que.pop();
vis[k] = 0;
}
if (len[t] == maxn) {
cout << -1 << endl;
}
else {
cout << len[t] << endl;
}
}
return 0;
}