Skip to content

Commit 09575db

Browse files
committed
Segment Tree updated
1 parent b6ed405 commit 09575db

7 files changed

+1022
-395
lines changed
Lines changed: 134 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,32 @@
11

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 ? **/
113

124
#include<bits/stdc++.h>
135
using namespace std;
146

157
#define LL long long
168
#define PII pair<int,int>
179
#define PLL pair<LL,LL>
18-
#define MP make_pair
1910
#define F first
2011
#define S second
2112

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)
2816

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
3323

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);
4330

4431
inline void optimizeIO()
4532
{
@@ -48,102 +35,160 @@ inline void optimizeIO()
4835
}
4936

5037
const int nmax = 2e5+7;
51-
const int INF = 1e9;
52-
const LL LINF = 1e17;
38+
const int INF = 1e9+7;
5339

54-
string to_str(LL x)
40+
struct Graph
5541
{
56-
stringstream ss;
57-
ss<<x;
58-
return ss.str();
59-
}
42+
int n;
43+
bool dir;
44+
vector<vector<PII>>adj;
6045

61-
//bool cmp(const PII &A,const PII &B)
62-
//{
63-
//
64-
//}
46+
vector<int>dist;
47+
vector<int>par;
6548

66-
int n,m;
49+
Graph(int n,bool dir)
50+
{
51+
this->n = n;
52+
this->dir = dir;
53+
int len = n+1;
6754

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+
}
7159

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+
}
7765

78-
while(!PQ.empty())
66+
void dijkstra(int s)
7967
{
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});
8271

83-
for(auto x:adj[now])
72+
while(!PQ.empty())
8473
{
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();
8777

88-
if(dist[now] + ed < dist[next])
78+
if(now_d > dist[now]) continue; /// OPTIMIZATIONNNN
79+
80+
for(auto x:adj[now])
8981
{
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+
}
9391
}
9492
}
9593
}
9694

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+
}
99102

100-
}
103+
cout<<dist[t]<<endl;
101104

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);
106107

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;
111119
}
112120

113-
reverse(ALL(path));
114121

115-
for(int x:path)
116-
cout<<x<<" ";
117-
cout<<endl;
118-
}
122+
void solve()
123+
{
119124

120-
int main()
121-
{
122-
optimizeIO();
125+
}
126+
};
123127

128+
void solveTC()
129+
{
130+
int n,m;
124131
cin>>n>>m;
125132

133+
Graph g(n,false); /// undirected
134+
126135
for(int i=0; i<m; i++)
127136
{
128-
int a,b,w;
129-
cin>>a>>b>>w;
137+
int a,b,c;
138+
cin>>a>>b>>c;
130139

131-
adj[a].push_back({b,w}); /** Directed **/
132-
//adj[b].push_back({a,w});
140+
g.add_edge(a,b,c);
133141
}
134142

135-
while(true)
136-
{
137-
int v;
138-
cin>>v;
139-
dijkstra(v);
140-
get_sp(v,n);
143+
g.solve();
144+
}
141145

142-
}
146+
int32_t main()
147+
{
148+
optimizeIO();
143149

150+
int tc = 1;
151+
// cin>>tc;
144152

153+
while(tc--)
154+
{
155+
solveTC();
156+
}
145157

146158
return 0;
147159
}
148160

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+
}
149194

0 commit comments

Comments
 (0)