forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
84 lines (75 loc) · 1.51 KB
/
B.cpp
File metadata and controls
84 lines (75 loc) · 1.51 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1LL << 60;
struct Edge
{
int y, z, id;
};
int n, m, tr[100100], id[100100], flag[200200];
long long dist[100100];
vector <Edge> a[100100];
void dijkstra()
{
int done[100100] = {0};
for (int i = 1; i <= n; i++)
dist[i] = oo;
dist[1] = 0;
priority_queue < pair<long long,int> > q;
q.push({0, 1});
while (!q.empty())
{
int x = q.top().second;
q.pop();
if (done[x]++) continue;
for (auto u : a[x])
if (!done[u.y] && dist[u.y] > dist[x] + u.z)
{
dist[u.y] = dist[x] + u.z;
tr[u.y] = u.id;
q.push({-dist[u.y], u.y});
}
}
}
bool cmp(int u, int v)
{
return dist[u] < dist[v];
}
int main()
{
freopen("algoritm.in", "r", stdin);
freopen("algoritm.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int test, x, y, z;
cin >> test;
while (test--)
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
a[i].clear();
id[i] = i;
}
for (int i = 1; i <= m; i++)
{
cin >> x >> y >> z;
a[x].push_back({y, z, i});
flag[i] = 0;
}
dijkstra();
sort(id + 1, id + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
int x = id[i], y = tr[x];
if (y && !flag[y])
{
flag[y] = 1;
cout << y << ' ';
}
}
for (int i = 1; i <= m; i++)
if (!flag[i])
cout << i << ' ';
cout << endl;
}
}