mirror of
https://git.sb/baoshuo/OI-codes.git
synced 2024-12-25 06:31:59 +00:00
P3371 【模板】单源最短路径(弱化版)
R53521521
This commit is contained in:
parent
b0dd339cbf
commit
fc84145098
@ -2,38 +2,39 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int n, m, s, x, y, z, g[10005][10005], dist[10005];
|
int n, m, s, u, v, w, dist[200005];
|
||||||
bool st[10005];
|
vector<pair<int, int>> g[200005];
|
||||||
|
bool st[200005];
|
||||||
|
|
||||||
void diskstra() {
|
void dijkstra() {
|
||||||
memset(dist, 0x3f, sizeof(dist));
|
memset(dist, 0x3f, sizeof(dist));
|
||||||
dist[s] = 0;
|
dist[s] = 0;
|
||||||
for (int i = 0; i < n - 1; i++) {
|
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
|
||||||
int t = -1;
|
q.push(make_pair(0, s));
|
||||||
for (int j = 1; j <= n; j++) {
|
while (!q.empty()) {
|
||||||
if (!st[j] && (t == -1 || dist[t] > dist[j])) {
|
auto t = q.top();
|
||||||
t = j;
|
q.pop();
|
||||||
|
int tw = t.second;
|
||||||
|
int td = t.first;
|
||||||
|
if (st[tw]) continue;
|
||||||
|
for (auto i : g[tw]) {
|
||||||
|
int j = i.first;
|
||||||
|
if (dist[j] > td + i.second) {
|
||||||
|
dist[j] = td + i.second;
|
||||||
|
q.push(make_pair(dist[j], j));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (int j = 1; j <= n; j++) {
|
st[tw] = true;
|
||||||
dist[j] = min(dist[j], dist[t] + g[t][j]);
|
|
||||||
}
|
|
||||||
st[t] = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
cin >> n >> m >> s;
|
cin >> n >> m >> s;
|
||||||
for (int i = 1; i <= n; i++) {
|
|
||||||
for (int j = 1; j <= n; j++) {
|
|
||||||
g[i][j] = i == j ? 0 : 0x3f3f3f3f;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 1; i <= m; i++) {
|
for (int i = 1; i <= m; i++) {
|
||||||
cin >> x >> y >> z;
|
cin >> u >> v >> w;
|
||||||
g[x][y] = min(g[x][y], z);
|
g[u].push_back(make_pair(v, w));
|
||||||
}
|
}
|
||||||
diskstra();
|
dijkstra();
|
||||||
for (int i = 1; i <= n; i++) {
|
for (int i = 1; i <= n; i++) {
|
||||||
cout << (dist[i] == 0x3f3f3f3f ? INT_MAX : dist[i]) << ' ';
|
cout << (dist[i] == 0x3f3f3f3f ? INT_MAX : dist[i]) << ' ';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user