-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
62 lines (59 loc) · 1.27 KB
/
dijkstra.cpp
File metadata and controls
62 lines (59 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define MAX 100
const int INF = 1e9;
vector<vector<pair<int, int>>>graph;
vector<int>dist(MAX, INF);
int path[MAX];
struct option
{
bool operator()(const pair<int, int>& a, const pair<int, int>& b) const
{
return a.second > b.second;
}
};
void Dijkstra(int s)
{
priority_queue < pair<int, int>, vector<pair<int, int>>, option>pq;
pq.push(make_pair(s, 0));
dist[s] = 0;
while (!pq.empty())
{
pair<int, int>top = pq.top();
pq.pop();
int u = top.first;
int w = top.second;
for (int i = 0; i < graph[u].size(); ++i)
{
pair<int, int>neighbor = graph[u][i];
if (w + neighbor.second < dist[neighbor.first])
{
dist[neighbor.first] = w + neighbor.second;
pq.push(pair<int, int>(neighbor.first, dist[neighbor.first]));
path[neighbor.first] = u;
}
}
}
}
int main()
{
int n, s, t;
dist = vector<int>(MAX, INF);
cin >> n;
s = 0; t = 4;
graph = vector<vector<pair<int, int>>>(MAX + 5, vector<pair<int, int>>());
int d = 0;
for(int i=0;i<n;i++)
for (int j = 0; j < n; j++)
{
cin >> d;
if (d > 0)
graph[i].push_back(pair<int, int>(j, d));
}
Dijkstra(s);
int ans = dist[t];
cout << ans;
return 0;
}