This is my code. It is for solving single-source shortest path problem using Dijkstra algorithm.
#include <cstdio>
using namespace std;
int mp[2505][2505];
int n, m, s, t;
const int INF = INT_MAX/4;
int main()
{
// freopen(".\\IO\\stdin.in", "r", stdin);
for (int i=0; i<2505; i++)
for (int j=0; j<2505; j++)
mp[i][j] = i==j ? 0 : INF;
scanf("%d%d%d%d", &n, &m, &s, &t);
for (int i=0; i<m; i++)
{
int u, v, t;
scanf("%d%d%d", &u, &v, &t);
mp[u][v] = mp[v][u] = t;
}
int dis[3000];
for (int i=1; i<=n; i++)
dis[i] = INF;
dis[s] = 0;
bool walked[2505];
memset(walked, 0, sizeof(walked));
for (int i=1; i<=n; i++)
{
int minN = -1, minV = INF;
for (int j=1; j<=n; j++)
{
if (walked[j]) continue;
if (dis[j] < minV)
minV=dis[j], minN=j;
}
for (int p=1; p<=n; p++)
if (dis[minN] + mp[minN][p] < dis[p])
dis[p] = dis[minN] + mp[minN][p];
walked[minN] = 1;
}
printf("%d\n", dis[t]);
return 0;
}
I tried to paste the input data in console, and got the right answer 1559
.
But when I wrote freopen(".\\IO\\stdin.in", "r", stdin);
(just like the annotation at the beginning of the main()
), put the data in the correct file and runs the program, it prints 0
.
Input data has 6000+ lines, so I put it at ubuntu pastebin. https://pastebin.ubuntu.com/p/7WhKQW2pZj/
Other info:
Windows 10
Complie command: g++ a.cpp -o a.exe -Wall
C++ standard: C++11