-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdijkstra.cpp
More file actions
55 lines (55 loc) · 1.11 KB
/
Copy pathdijkstra.cpp
File metadata and controls
55 lines (55 loc) · 1.11 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
/// https://infoarena.ro/problema/dijkstra
#include<fstream>
#include<vector>
#include<queue>
#define INF 2000000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct elem
{
int x,c;
bool operator < (const elem &a) const
{
return c>a.c;
}
};
vector<elem>a[50002];
priority_queue<elem>pq;
int sol[50002];
int main()
{
int n,m,i,x,y,z,l;
elem p;
f>>n>>m;
for(i=1;i<=m;i++)
{
f>>x>>y>>z;
a[x].push_back({y,z});
a[y].push_back({y,z});
}
for(i=2;i<=n;i++)
sol[i]=INF;
pq.push({1,0});
while(!pq.empty())
{
p=pq.top();
pq.pop();
if(p.c==sol[p.x])
{
l=a[p.x].size();
for(i=0;i<l;i++)
if(sol[a[p.x][i].x]>sol[p.x]+a[p.x][i].c)
{
sol[a[p.x][i].x]=sol[p.x]+a[p.x][i].c;
pq.push({a[p.x][i].x,sol[a[p.x][i].x]});
}
}
}
for(i=2;i<=n;i++)
if(sol[i]==INF)
g<<0<<" ";
else
g<<sol[i]<<" ";
return 0;
}