Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eb34ab5

Browse files
Added problems
1 parent e1efe2b commit eb34ab5

File tree

3 files changed

+238
-0
lines changed

3 files changed

+238
-0
lines changed
File renamed without changes.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long int li;
6+
typedef long long int lli;
7+
typedef vector<long long int> vlli;
8+
typedef vector<int> vi;
9+
typedef stack<long long int> slli;
10+
typedef pair<lli, lli> plli;
11+
12+
class Graph
13+
{
14+
15+
vector<vector<plli>> adj;
16+
lli nodes;
17+
18+
public:
19+
Graph(lli nodes)
20+
{
21+
this->adj.resize(nodes, vector<plli>{});
22+
this->nodes = nodes;
23+
}
24+
25+
void addDirectedEdge(lli src, lli dst, lli weight)
26+
{
27+
28+
if (src == dst)
29+
{
30+
return;
31+
}
32+
33+
this->adj[src].emplace_back(make_pair(dst, weight));
34+
}
35+
36+
void dijkstra()
37+
{
38+
39+
vector<lli> distances(this->nodes, LONG_LONG_MAX);
40+
41+
distances[0] = 0;
42+
43+
priority_queue<plli, vector<plli>, greater<plli>> pq;
44+
45+
pq.push(make_pair(0, 0));
46+
47+
while (!pq.empty())
48+
{
49+
50+
auto top = pq.top();
51+
pq.pop();
52+
53+
lli u = top.second;
54+
55+
// important for speeding up
56+
57+
if (top.first > distances[u])
58+
{
59+
continue;
60+
}
61+
62+
for (auto neighbour : this->adj[u])
63+
{
64+
65+
lli v = neighbour.first;
66+
lli w = neighbour.second;
67+
68+
if (distances[v] > distances[u] + w)
69+
{
70+
distances[v] = distances[u] + w;
71+
pq.push(make_pair(distances[v], v));
72+
}
73+
}
74+
}
75+
76+
for (auto distance : distances)
77+
{
78+
cout << distance << ' ';
79+
}
80+
81+
cout << '\n';
82+
}
83+
};
84+
85+
void task()
86+
{
87+
88+
lli cities, flights;
89+
cin >> cities >> flights;
90+
91+
lli src, dst, weight;
92+
93+
Graph g(cities);
94+
95+
for (lli i = 0; i < flights; i++)
96+
{
97+
cin >> src >> dst >> weight;
98+
g.addDirectedEdge(src - 1, dst - 1, weight);
99+
}
100+
101+
g.dijkstra();
102+
}
103+
104+
int main()
105+
{
106+
107+
ios::sync_with_stdio(0);
108+
cin.tie(0);
109+
110+
task();
111+
112+
return 0;
113+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
typedef long int li;
6+
typedef long long int lli;
7+
typedef vector<long long int> vlli;
8+
typedef vector<int> vi;
9+
typedef stack<long long int> slli;
10+
typedef pair<lli, lli> plli;
11+
12+
class Graph
13+
{
14+
15+
vector<vector<lli>> adj;
16+
lli nodes;
17+
18+
public:
19+
Graph(lli nodes)
20+
{
21+
this->adj.resize(nodes, vector<lli>(nodes, LONG_LONG_MAX));
22+
23+
for (lli i = 0; i < nodes; i++)
24+
{
25+
this->adj[i][i] = 0;
26+
}
27+
28+
this->nodes = nodes;
29+
}
30+
31+
void addUndirectedEdge(lli src, lli dst, lli weight)
32+
{
33+
34+
if (src == dst)
35+
{
36+
return;
37+
}
38+
39+
if (this->adj[src][dst] > weight)
40+
{
41+
this->adj[src][dst] = weight;
42+
this->adj[dst][src] = weight;
43+
}
44+
}
45+
46+
void floydWarshall()
47+
{
48+
49+
for (lli k = 0; k < this->nodes; k++)
50+
{
51+
52+
for (lli i = 0; i < this->nodes; i++)
53+
{
54+
55+
for (lli j = 0; j < this->nodes; j++)
56+
{
57+
58+
if (this->adj[i][k] == LONG_LONG_MAX || this->adj[k][j] == LONG_LONG_MAX)
59+
{
60+
continue;
61+
}
62+
63+
if (this->adj[i][k] + this->adj[k][j] < this->adj[i][j])
64+
{
65+
66+
this->adj[i][j] = this->adj[i][k] + this->adj[k][j];
67+
}
68+
}
69+
}
70+
}
71+
}
72+
73+
lli shortestDistance(lli src, lli dst)
74+
{
75+
76+
if (this->adj[src][dst] == LONG_LONG_MAX)
77+
{
78+
return -1;
79+
}
80+
return this->adj[src][dst];
81+
}
82+
};
83+
84+
void task()
85+
{
86+
87+
lli cities, roads, lenQueries;
88+
cin >> cities >> roads >> lenQueries;
89+
90+
Graph g(cities);
91+
92+
lli src, dst, weight;
93+
94+
for (lli i = 0; i < roads; i++)
95+
{
96+
cin >> src >> dst >> weight;
97+
g.addUndirectedEdge(src - 1, dst - 1, weight);
98+
}
99+
100+
g.floydWarshall();
101+
102+
vector<lli> res;
103+
104+
for (lli i = 0; i < lenQueries; i++)
105+
{
106+
cin >> src >> dst;
107+
res.emplace_back(g.shortestDistance(src - 1, dst - 1));
108+
}
109+
110+
for (auto ans : res)
111+
{
112+
cout << ans << '\n';
113+
}
114+
}
115+
116+
int main()
117+
{
118+
119+
ios::sync_with_stdio(0);
120+
cin.tie(0);
121+
122+
task();
123+
124+
return 0;
125+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /