#include <bits/stdc++.h>
#define int long long
#define fileopen(a, b) freopen(((std::string)a + ".inp").c_str(), "r", stdin); freopen(((std::string)b + ".out").c_str(), "w", stdout);
#define fileopen1(a) freopen(((std::string)a + ".inp").c_str(), "r", stdin); freopen(((std::string)a + ".out").c_str(), "w", stdout);
using namespace std;
const int MAXN = 205;
struct edge {
int u, v, t, c;
};
#define ld long double
const ld EPS = 1e-9;
ld q;
ld num(edge a) {
return q * a.t + (1 - q) * a.c;
}
bool cmp(edge a, edge b) {
return num(a) + EPS < num(b);
}
vector<edge> edges;
int n, m;
int dsu[MAXN];
int Find(int u) {
if (u == dsu[u]) return u;
return dsu[u] = Find(dsu[u]);
}
void Union(int u, int v) {
u = Find(u), v = Find(v);
if (u != v) dsu[v] = u;
}
int solve(ld okayge) {
q = okayge;
sort(edges.begin(), edges.end(), cmp);
for (int i = 1; i <= n; i++) dsu[i] = i;
int tott = 0, totc = 0;
for (auto e: edges) {
int u = e.u, v = e.v;
if (Find(u) != Find(v)) {
Union(u, v);
tott += e.t;
totc += e.c;
}
}
return tott * totc;
}
void print(ld okayge) {
q = okayge;
sort(edges.begin(), edges.end(), cmp);
for (int i = 1; i <= n; i++) dsu[i] = i;
int tott = 0, totc = 0;
for (auto e: edges) {
int u = e.u, v = e.v;
if (Find(u) != Find(v)) {
Union(u, v);
tott += e.t;
totc += e.c;
}
}
cout << tott << ' ' << totc << '\n';
for (int i = 1; i <= n; i++) dsu[i] = i;
for (auto e: edges) {
int u = e.u, v = e.v;
if (Find(u) != Find(v)) {
Union(u, v);
cout << u - 1 << ' ' << v - 1 << '\n';
}
}
}
signed main() {
#ifndef PICHU_LOCAL_DEF
//fileopen1("LAH");
#endif
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
q = 0.1;
for (int i = 1, u, v, t, c; i <= m; i++) {
cin >> u >> v >> t >> c;
u++; v++;
edges.push_back({u, v, t, c});
}
ld l = 0, r = 1;
while (r - l > 1e-8) {
ld lm = l + (r - l) / 3, rm = r - (r - l) / 3;
// cout << lm << ' ' << rm << ' ' << solve(lm) << ' ' << solve(rm) << '\n';
if (solve(lm) > solve(rm)) l = lm;
else r = rm;
}
if (solve(l) < solve(r)) print(l);
else print(r);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
5 ms |
332 KB |
Output is correct |
6 |
Correct |
3 ms |
332 KB |
Output is correct |
7 |
Correct |
21 ms |
444 KB |
Output is correct |
8 |
Correct |
95 ms |
976 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
5 ms |
332 KB |
Output is correct |
15 |
Correct |
4 ms |
332 KB |
Output is correct |
16 |
Correct |
21 ms |
440 KB |
Output is correct |
17 |
Correct |
25 ms |
440 KB |
Output is correct |
18 |
Correct |
25 ms |
332 KB |
Output is correct |
19 |
Correct |
159 ms |
848 KB |
Output is correct |
20 |
Correct |
128 ms |
848 KB |
Output is correct |