//
// main.cpp
// 最短路问题_1
//
// Created by 游子墨 on 2024/12/28.
//
#include <iostream>
#include <vector>
#include <queue>
#define MAXN 2005
#define INF 2e18
using namespace std;
struct Nod {
int u;
long long len;
Nod(int u_, long long len_) {
u = u_;
len = len_;
}
};
bool operator>(Nod x, Nod y) {
if (x.len != y.len) return x.len > y.len;
return x.u > y.u;
}
long long dis[MAXN]; // distance
bool vis[MAXN];
vector<int> G[MAXN]; // This array is used to store the map.
vector<long long> val[MAXN]; // This array is used to store the length of edges.
int main(int argc, const char * argv[]) {
// insert code here...
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++) { // Input the data.
int u, v;
long long w;
cin >> u >> v >> w;
G[u].push_back(v);
val[u].push_back(w);
}
for (int i = 1; i <= n; i++) { // Initialize the 'dis' array.
dis[i] = INF;
}
priority_queue<Nod,vector<Nod>,greater<Nod> > q;
q.push(Nod(1, 0));
dis[1] = 0;
while(!q.empty()) { // Use Dijkstra's algorithm to obtain the answer.
int x = q.top().u; q.pop();
if (vis[x]) continue;
vis[x] = true;
for (int i = 0; i < G[x].size(); i++) {
int v = G[x][i];
dis[v] = min(dis[v], dis[x] + val[x][i]);
q.push(Nod(v, dis[v]));
}
}
for (int i = 1; i <= n; i++) { // Output the distance data.
if (dis[i] >= INF) cout << "-1 ";
else cout << dis[i] << ' ';
}
return 0; // The program ends.
}
提交结果:
WA on #10.
错误信息:
Wrong Answer.wrong answer On line 1 column 8, read 2, expected -.
提交记录: