Skip to content

Commit 3ddb67d

Browse files
committed
new: France-IOI Plus courts chemins
1 parent 7a24c1d commit 3ddb67d

9 files changed

+416
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
ios::sync_with_stdio(false);
6+
cin.tie(0);
7+
cout.tie(0);
8+
9+
int n, a;
10+
cin >> n >> a;
11+
12+
vector<vector<int>> d(n, vector<int>(n, INT_MAX));
13+
for (int i = 0; i < a; i++) {
14+
int a, b, w;
15+
cin >> a >> b >> w;
16+
d[a - 1][b - 1] = w;
17+
d[b - 1][a - 1] = w;
18+
}
19+
for (int i = 0; i < n; i++) d[i][i] = 0;
20+
21+
for (int k = 0; k < n; k++) {
22+
for (int i = 0; i < n; i++) {
23+
for (int j = 0; j < n; j++) {
24+
if (d[i][k] < INT_MAX && d[k][j] < INT_MAX)
25+
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
26+
}
27+
}
28+
}
29+
30+
for (int i = 0; i < n; i++) {
31+
d[i][i] = 0;
32+
for (int j = 0; j < n; j++) cout << d[i][j] << " ";
33+
cout << "\n";
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
4+
int n, k, a;
5+
typedef pair<long long, int> state;
6+
vector<state> st;
7+
vector<map<int, int>> adj;
8+
9+
int main() {
10+
ios::sync_with_stdio(false);
11+
12+
cin >> n >> a >> k;
13+
k--;
14+
adj.resize(n, map<int, int>());
15+
st.resize(n);
16+
17+
for (int i = 0; i < n; i++) st[i] = make_pair(LLONG_MAX, i);
18+
19+
for (int i = 0; i < a; i++) {
20+
int a, b, d;
21+
cin >> a >> b >> d;
22+
adj[a - 1][b - 1] = d;
23+
adj[b - 1][a - 1] = d;
24+
}
25+
26+
priority_queue<state, vector<state>, greater<state>> q;
27+
q.push(make_pair(0, k));
28+
while (!q.empty()) {
29+
long long d;
30+
int v;
31+
tie(d, v) = q.top();
32+
q.pop();
33+
34+
if (st[v].first <= d) continue;
35+
st[v].first = d;
36+
37+
for (auto entry : adj[v]) {
38+
int c, dc;
39+
tie(c, dc) = entry;
40+
q.push(make_pair(d + dc, c));
41+
}
42+
}
43+
44+
sort(st.begin(), st.end(), [](const state a, const state b) {
45+
return a.first == b.first ? a.second < b.second : a.first < b.first;
46+
});
47+
while (st.back().first == LLONG_MAX) st.pop_back();
48+
49+
for (auto i : st) cout << i.first << " " << i.second + 1 << "\n";
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
4+
int n, k, a;
5+
typedef pair<long long, int> state;
6+
vector<state> st;
7+
vector<map<int, int>> adj;
8+
9+
int main() {
10+
ios::sync_with_stdio(false);
11+
12+
cin >> n >> a >> k;
13+
k--;
14+
adj.resize(n, map<int, int>());
15+
st.resize(n);
16+
17+
for (int i = 0; i < n; i++) st[i] = make_pair(LLONG_MAX, i);
18+
19+
for (int i = 0; i < a; i++) {
20+
int a, b, d;
21+
cin >> a >> b >> d;
22+
adj[a - 1][b - 1] = d;
23+
adj[b - 1][a - 1] = d;
24+
}
25+
26+
priority_queue<state, vector<state>, greater<state>> q;
27+
q.push(make_pair(0, k));
28+
while (!q.empty()) {
29+
long long d;
30+
int v;
31+
tie(d, v) = q.top();
32+
q.pop();
33+
34+
if (st[v].first <= d) continue;
35+
st[v].first = d;
36+
37+
for (auto entry : adj[v]) {
38+
int c, dc;
39+
tie(c, dc) = entry;
40+
q.push(make_pair(d + dc, c));
41+
}
42+
}
43+
44+
sort(st.begin(), st.end(), [](const state a, const state b) {
45+
return a.first == b.first ? a.second < b.second : a.first < b.first;
46+
});
47+
while (st.back().first == LLONG_MAX) st.pop_back();
48+
49+
for (auto i : st) cout << i.first << " " << i.second + 1 << "\n";
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
4+
int n, k, a;
5+
typedef pair<long long, int> state;
6+
vector<state> st;
7+
vector<map<int, int>> adj;
8+
9+
int main() {
10+
ios::sync_with_stdio(false);
11+
12+
cin >> n >> a >> k;
13+
k--;
14+
adj.resize(n, map<int, int>());
15+
st.resize(n);
16+
17+
for (int i = 0; i < n; i++) st[i] = make_pair(LLONG_MAX, i);
18+
19+
for (int i = 0; i < a; i++) {
20+
int a, b, d;
21+
cin >> a >> b >> d;
22+
adj[a - 1][b - 1] = d;
23+
adj[b - 1][a - 1] = d;
24+
}
25+
26+
priority_queue<state, vector<state>, greater<state>> q;
27+
q.push(make_pair(0, k));
28+
while (!q.empty()) {
29+
long long d;
30+
int v;
31+
tie(d, v) = q.top();
32+
q.pop();
33+
34+
if (st[v].first <= d) continue;
35+
st[v].first = d;
36+
37+
for (auto entry : adj[v]) {
38+
int c, dc;
39+
tie(c, dc) = entry;
40+
q.push(make_pair(d + dc, c));
41+
}
42+
}
43+
44+
sort(st.begin(), st.end(), [](const state a, const state b) {
45+
return a.first == b.first ? a.second < b.second : a.first < b.first;
46+
});
47+
while (st.back().first == LLONG_MAX) st.pop_back();
48+
49+
for (auto i : st) cout << i.first << " " << i.second + 1 << "\n";
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
4+
const pair<int, int> d[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
5+
6+
int l, c;
7+
vector<vector<char>> input;
8+
vector<vector<int>> st;
9+
10+
void compute_weights() {
11+
priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
12+
greater<tuple<int, int, int>>>
13+
q;
14+
q.push(make_tuple(0, 1, 0));
15+
while (!q.empty()) {
16+
int ans, x, y;
17+
tie(ans, x, y) = q.top();
18+
q.pop();
19+
20+
// out of bounds
21+
if (x < 0 || x >= l || y < 0 || y >= c) continue;
22+
// not crossable
23+
if (input[x][y] < '0') continue;
24+
25+
// already reached this position with a lower score
26+
ans += input[x][y] - '0';
27+
if (st[x][y] <= ans) continue;
28+
st[x][y] = ans;
29+
30+
for (auto di : d) {
31+
int dx, dy;
32+
tie(dx, dy) = di;
33+
auto next = make_tuple(ans, x + dx, y + dy);
34+
q.push(next);
35+
}
36+
}
37+
}
38+
39+
void print() {
40+
for (int i = 0; i < l; i++) {
41+
for (int j = 0; j < c; j++) {
42+
cout << setw(10) << setfill('0') << st[i][j] << " ";
43+
}
44+
cout << "\n";
45+
}
46+
}
47+
48+
int main() {
49+
ios::sync_with_stdio(false);
50+
51+
cin >> l >> c;
52+
input.assign(l, vector<char>(c));
53+
st.assign(l, vector<int>(c, INT_MAX));
54+
for (int i = 0; i < l; i++) {
55+
string s;
56+
cin >> s;
57+
for (int j = 0; j < c; j++) input[i][j] = (s[j] == '.' ? '0' : s[j]);
58+
}
59+
compute_weights();
60+
// print();
61+
62+
cout << st[l - 1][c - 2];
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define pii pair<int, int>
4+
5+
int main() {
6+
ios::sync_with_stdio(false);
7+
cin.tie(0);
8+
cout.tie(0);
9+
10+
int n, a;
11+
cin >> n >> a;
12+
13+
vector<map<int, int>> adj(n);
14+
for (int i = 0; i < a; i++) {
15+
int a, b, d;
16+
cin >> a >> b >> d;
17+
adj[a - 1][b - 1] = d;
18+
}
19+
20+
vector<int> d(n, INT_MAX);
21+
d[0] = 0;
22+
bool test = false;
23+
for (int i = 0; i < n; i++) {
24+
test = false;
25+
for (int v = 0; v < n; v++) {
26+
for (auto e : adj[v]) {
27+
if (d[v] < INT_MAX && d[v] + e.second < d[e.first])
28+
d[e.first] = d[v] + e.second, test = true;
29+
}
30+
}
31+
if (!test) break;
32+
}
33+
34+
if (!test)
35+
cout << d.back();
36+
else
37+
cout << "ERREUR";
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define tiii tuple<int, int, int>
4+
5+
int main() {
6+
ios::sync_with_stdio(false);
7+
cin.tie(0);
8+
cout.tie(0);
9+
10+
int n, a, s, f;
11+
cin >> n >> a >> s >> f;
12+
s--, f--;
13+
vector<map<int, int>> adj(n);
14+
15+
for (int i = 0; i < a; i++) {
16+
int a, b, d;
17+
cin >> a >> b >> d;
18+
adj[a - 1][b - 1] = d;
19+
adj[b - 1][a - 1] = d;
20+
}
21+
22+
vector<int> st(n, INT_MAX), p(n, -1);
23+
24+
priority_queue<tiii, vector<tiii>, greater<tiii>> q;
25+
q.push(make_tuple(0, s, s));
26+
while (!q.empty()) {
27+
int v, d, pv;
28+
tie(d, v, pv) = q.top();
29+
q.pop();
30+
31+
if (st[v] <= d) continue;
32+
p[v] = pv;
33+
st[v] = d;
34+
35+
for (auto entry : adj[v]) {
36+
int c, dc;
37+
tie(c, dc) = entry;
38+
q.push(make_tuple(d + dc, c, v));
39+
}
40+
}
41+
42+
vector<int> path;
43+
for (int cur = f; cur != s; cur = p[cur]) path.push_back(cur);
44+
path.push_back(s);
45+
reverse(path.begin(), path.end());
46+
47+
cout << st[f] << " " << path.size() << "\n";
48+
for (auto i : path) cout << i + 1 << " ";
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define pii pair<int, int>
4+
5+
int main() {
6+
ios::sync_with_stdio(false);
7+
cin.tie(0);
8+
cout.tie(0);
9+
10+
int n, a, s, t;
11+
cin >> n >> a >> s >> t;
12+
s--;
13+
vector<unordered_map<int, int>> adj(n);
14+
15+
for (int i = 0; i < a; i++) {
16+
int a, b, d;
17+
cin >> a >> b >> d;
18+
adj[a - 1][b - 1] = d;
19+
}
20+
21+
vector<int> d(n, INT_MIN), tmp;
22+
d[s] = 0;
23+
auto ans = 0;
24+
while (t--) {
25+
tmp.assign(n, INT_MIN);
26+
for (int v = 0; v < n; v++) {
27+
if (d[v] == INT_MIN) continue;
28+
for (auto e : adj[v]) {
29+
int c, w;
30+
tie(c, w) = e;
31+
ans = max(ans, d[v] + w);
32+
tmp[c] = max(tmp[c], d[v] + w);
33+
}
34+
}
35+
36+
d.swap(tmp);
37+
}
38+
39+
cout << ans;
40+
}

0 commit comments

Comments
 (0)