1
1
2
- /* *
3
-
4
- Complexity : O(ElogV)
5
-
6
- Note : No guarantee of working with negative weight , does not work with negative cycle either
7
-
8
- **/
9
-
10
- /* *Which of the favors of your Lord will you deny ?**/
2
+ /* * Which of the favors of your Lord will you deny ? **/
11
3
12
4
#include < bits/stdc++.h>
13
5
using namespace std ;
14
6
15
7
#define LL long long
16
8
#define PII pair<int ,int >
17
9
#define PLL pair<LL,LL>
18
- #define MP make_pair
19
10
#define F first
20
11
#define S second
21
12
22
- #define ALL (x ) (x).begin(), (x).end()
23
- #define DBG (x ) cerr << __LINE__ << " says: " << #x << " = " << (x) << endl
24
-
25
- #include < ext/pb_ds/assoc_container.hpp>
26
- #include < ext/pb_ds/tree_policy.hpp>
27
- using namespace __gnu_pbds ;
13
+ #define ALL (x ) (x).begin(), (x).end()
14
+ #define READ freopen (" alu.txt" , " r" , stdin)
15
+ #define WRITE freopen (" vorta.txt" , " w" , stdout)
28
16
29
- template <class TIn >
30
- using indexed_set = tree<
31
- TIn, null_type, less<TIn>,
32
- rb_tree_tag, tree_order_statistics_node_update>;
17
+ #ifndef ONLINE_JUDGE
18
+ #define DBG (x ) cout << __LINE__ << " says: " << #x << " = " << (x) << endl
19
+ #else
20
+ #define DBG (x )
21
+ #define endl " \n "
22
+ #endif
33
23
34
- /*
35
- PBDS
36
- -------------------------------------------------
37
- 1) insert(value)
38
- 2) erase(value)
39
- 3) order_of_key(value) // 0 based indexing
40
- 4) *find_by_order(position) // 0 based indexing
41
-
42
- */
24
+ template <class T1 , class T2 >
25
+ ostream &operator <<(ostream &os, pair<T1,T2>&p);
26
+ template <class T >
27
+ ostream &operator <<(ostream &os, vector<T>&v);
28
+ template <class T >
29
+ ostream &operator <<(ostream &os, set<T>&v);
43
30
44
31
inline void optimizeIO ()
45
32
{
@@ -48,102 +35,160 @@ inline void optimizeIO()
48
35
}
49
36
50
37
const int nmax = 2e5 +7 ;
51
- const int INF = 1e9 ;
52
- const LL LINF = 1e17 ;
38
+ const int INF = 1e9 +7 ;
53
39
54
- string to_str (LL x)
40
+ struct Graph
55
41
{
56
- stringstream ss;
57
- ss<<x;
58
- return ss.str ();
59
- }
42
+ int n;
43
+ bool dir;
44
+ vector<vector<PII>>adj;
60
45
61
- // bool cmp(const PII &A,const PII &B)
62
- // {
63
- //
64
- // }
46
+ vector<int >dist;
47
+ vector<int >par;
65
48
66
- int n,m;
49
+ Graph (int n,bool dir)
50
+ {
51
+ this ->n = n;
52
+ this ->dir = dir;
53
+ int len = n+1 ;
67
54
68
- vector<PII>adj[nmax];
69
- vector<int >dist (nmax,INF);
70
- vector<int >par (nmax,-1 );
55
+ adj = vector<vector<PII>>(len);
56
+ dist = vector<int >(len,INF);
57
+ par = vector<int >(len,-1 );
58
+ }
71
59
72
- void dijkstra (int s )
73
- {
74
- priority_queue< PII,vector<PII>,greater<PII> >PQ ;
75
- dist[s] = 0 ;
76
- PQ. push ({ 0 ,s});
60
+ void add_edge (int u, int v, int c )
61
+ {
62
+ adj[u]. push_back ({v,c}) ;
63
+ if (!dir) adj[v]. push_back ({u,c}) ;
64
+ }
77
65
78
- while (!PQ. empty () )
66
+ void dijkstra ( int s )
79
67
{
80
- int now = PQ.top ().S ;
81
- PQ.pop ();
68
+ priority_queue< PII,vector<PII>,greater<PII> >PQ;
69
+ dist[s] = 0 ;
70
+ PQ.push ({0 ,s});
82
71
83
- for ( auto x:adj[now] )
72
+ while (!PQ. empty () )
84
73
{
85
- int next = x.F ;
86
- int ed = x.S ;
74
+ int now = PQ.top ().S ;
75
+ int now_d = PQ.top ().F ;
76
+ PQ.pop ();
87
77
88
- if (dist[now] + ed < dist[next])
78
+ if (now_d > dist[now]) continue ; // / OPTIMIZATIONNNN
79
+
80
+ for (auto x:adj[now])
89
81
{
90
- dist[next] = dist[now] + ed;
91
- par[next] = now;
92
- PQ.push ({dist[next],next});
82
+ int next = x.F ;
83
+ int ed = x.S ;
84
+
85
+ if (dist[now] + ed < dist[next])
86
+ {
87
+ dist[next] = dist[now] + ed;
88
+ par[next] = now;
89
+ PQ.push ({dist[next],next});
90
+ }
93
91
}
94
92
}
95
93
}
96
94
97
- for (int i=1 ; i<=n; i++)
98
- cout<<i<<" -> " <<dist[i]<<endl;
95
+ void get_sp (int t)
96
+ {
97
+ if (dist[t]>=INF)
98
+ {
99
+ cout<<" IMPOSSIBLE" <<endl;
100
+ return ;
101
+ }
99
102
100
- }
103
+ cout<<dist[t]<<endl;
101
104
102
- void get_sp (int s,int t)
103
- {
104
- vector<int >path;
105
- path.push_back (t);
105
+ vector<int >path;
106
+ path.push_back (t);
106
107
107
- while (par[t]!=-1 )
108
- {
109
- path.push_back (par[t]);
110
- t = par[t];
108
+ while (par[t]!=-1 )
109
+ {
110
+ path.push_back (par[t]);
111
+ t = par[t];
112
+ }
113
+
114
+ reverse (ALL (path));
115
+
116
+ for (int x:path)
117
+ cout<<x<<" " ;
118
+ cout<<endl;
111
119
}
112
120
113
- reverse (ALL (path));
114
121
115
- for (int x:path)
116
- cout<<x<<" " ;
117
- cout<<endl;
118
- }
122
+ void solve ()
123
+ {
119
124
120
- int main ()
121
- {
122
- optimizeIO ();
125
+ }
126
+ };
123
127
128
+ void solveTC ()
129
+ {
130
+ int n,m;
124
131
cin>>n>>m;
125
132
133
+ Graph g (n,false ); // / undirected
134
+
126
135
for (int i=0 ; i<m; i++)
127
136
{
128
- int a,b,w ;
129
- cin>>a>>b>>w ;
137
+ int a,b,c ;
138
+ cin>>a>>b>>c ;
130
139
131
- adj[a].push_back ({b,w}); /* * Directed **/
132
- // adj[b].push_back({a,w});
140
+ g.add_edge (a,b,c);
133
141
}
134
142
135
- while (true )
136
- {
137
- int v;
138
- cin>>v;
139
- dijkstra (v);
140
- get_sp (v,n);
143
+ g.solve ();
144
+ }
141
145
142
- }
146
+ int32_t main ()
147
+ {
148
+ optimizeIO ();
143
149
150
+ int tc = 1 ;
151
+ // cin>>tc;
144
152
153
+ while (tc--)
154
+ {
155
+ solveTC ();
156
+ }
145
157
146
158
return 0 ;
147
159
}
148
160
161
+ /* *
162
+
163
+ **/
164
+
165
+ template <class T1 , class T2 >
166
+ ostream &operator <<(ostream &os, pair<T1,T2>&p)
167
+ {
168
+ os<<" {" <<p.first <<" , " <<p.second <<" } " ;
169
+ return os;
170
+ }
171
+ template <class T >
172
+ ostream &operator <<(ostream &os, vector<T>&v)
173
+ {
174
+ os<<" [ " ;
175
+ for (T i:v)
176
+ {
177
+ os<<i<<" " ;
178
+ }
179
+ os<<" ]" ;
180
+ return os;
181
+ }
182
+
183
+ template <class T >
184
+ ostream &operator <<(ostream &os, set<T>&v)
185
+ {
186
+ os<<" [ " ;
187
+ for (T i:v)
188
+ {
189
+ os<<i<<" " ;
190
+ }
191
+ os<<" ]" ;
192
+ return os;
193
+ }
149
194
0 commit comments