#include <bits/stdc++.h>
using namespace std;
#define int long long
int ans = 0;
map<pair<int, int>, int> cost_of_pair;
vector<int> cost;
vector<int> calculated;
vector<vector<pair<int, int>>> g;
int dfs(int u, int depth) {
if(depth >= 2) {
return 0;
}
if(depth == 1) {
for(auto v : calculated) {
if(cost_of_pair.count(make_pair(u, v))) {
//cout << v << ' ' << u << '\n';
//cout << cost[v] << ' ' << cost[u] << ' ' << cost_of_pair[make_pair(u, v)] << '\n';
ans = max(ans, cost[v] + cost[u] + cost_of_pair[make_pair(u, v)]);
}
}
return 0;
}
int curr = 0;
for(auto v : g[u]) {
cost[v.first] = v.second;
calculated.push_back(v.first);
curr += v.second;
dfs(v.first, depth + 1);
}
return curr;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
cost = vector<int>(n);
g = vector<vector<pair<int, int>>>(n);
for(int i = 0; i < m; i++) {
int from, to, w;
cin >> from >> to >> w;
from--;
to--;
g[from].push_back(make_pair(to, w));
g[to].push_back(make_pair(from, w));
cost_of_pair[make_pair(from, to)] = w;
cost_of_pair[make_pair(to, from)] = w;
}
ans = 0;
for(int i = 0; i < n; i++) {
int curr = dfs(i, 0);
calculated.clear();
ans = max(ans, curr);
}
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
456 ms |
3800 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
768 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
456 ms |
3800 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
3 ms |
604 KB |
Output is correct |
19 |
Correct |
5 ms |
768 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Execution timed out |
3028 ms |
83724 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
38016 KB |
Output is correct |
2 |
Correct |
609 ms |
62528 KB |
Output is correct |
3 |
Correct |
127 ms |
21076 KB |
Output is correct |
4 |
Correct |
311 ms |
41296 KB |
Output is correct |
5 |
Correct |
1279 ms |
59892 KB |
Output is correct |
6 |
Execution timed out |
3052 ms |
20020 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
38016 KB |
Output is correct |
2 |
Correct |
609 ms |
62528 KB |
Output is correct |
3 |
Correct |
127 ms |
21076 KB |
Output is correct |
4 |
Correct |
311 ms |
41296 KB |
Output is correct |
5 |
Correct |
1279 ms |
59892 KB |
Output is correct |
6 |
Execution timed out |
3052 ms |
20020 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |