#include <cstdio>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
#define SZ(x) ((int)x.size())
//#define debug
struct edge {
int u, v, t, c;
edge(int _u, int _v, int _t, int _c) : u(_u), v(_v), t(_t), c(_c) {}
};
const int N = (int)205;
int n, m, f[N], mnt = 1e9, mnc = 1e9;
ii opt = ii(1e9, 1e9);
vector<edge> e;
int getf(int x) { return f[x] = f[x] == x ? x : getf(f[x]); }
bool mergef(int x, int y) {
int u = getf(x), v = getf(y);
if (u == v) return false;
f[v] = u;
return true;
}
ii get_mst(int ta, int ca, int mode) { //mode = 1 <=> output answer
for (int i = 0; i < n; ++i) f[i] = i;
sort(e.begin(), e.end(), [&] (const edge &x, const edge &y) {
return 1ll * x.t * ta + 1ll * x.c * ca < 1ll * y.t * ta + 1ll * y.c * ca;
});
int sumt = 0, sumc = 0;
for (int i = 0; i < m; ++i) {
if (mergef(e[i].u, e[i].v)) {
sumt += e[i].t;
sumc += e[i].c;
if (mode) printf("%d %d\n", e[i].u, e[i].v);
}
}
if (1ll * sumt * sumc < 1ll * mnt * mnc) {
mnt = sumt;
mnc = sumc;
opt = ii(ta, ca);
}
return ii(sumt, sumc);
}
bool ccw(const ii &a, const ii &b, const ii &c){
int dx1 = b.first - a.first;
int dy1 = b.second - a.second;
int dx2 = c.first - a.first;
int dy2 = c.second - a.second;
return 1ll * dx1 * dy2 >= 1ll * dy1 * dx2;
}
void solve(const ii &s, const ii &t) {
ii mid = get_mst(s.second - t.second, s.first - t.first, 0);
if (!ccw(t, mid, s)) {
solve(s, mid);
solve(mid, t);
}
}
int main() {
#ifdef debug
freopen("inp.txt", "rt", stdin);
#endif
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int u, v, t, c;
scanf("%d%d%d%d", &u, &v, &t, &c);
e.push_back(edge(u, v, t, c));
}
ii s = get_mst(1, 0, 0); //minimize t
ii t = get_mst(0, 1, 0); //minimize c
solve(s, t);
printf("%d %d\n", mnt, mnc);
get_mst(opt.first, opt.second, 1);
return 0;
}
Compilation message
timeismoney.cpp: In function 'int main()':
timeismoney.cpp:79:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
timeismoney.cpp:82:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &u, &v, &t, &c);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
248 KB |
Output is correct |
2 |
Correct |
2 ms |
352 KB |
Output is correct |
3 |
Correct |
2 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
496 KB |
Output is correct |
5 |
Correct |
2 ms |
496 KB |
Output is correct |
6 |
Correct |
2 ms |
520 KB |
Output is correct |
7 |
Correct |
3 ms |
520 KB |
Output is correct |
8 |
Correct |
8 ms |
900 KB |
Output is correct |
9 |
Correct |
2 ms |
900 KB |
Output is correct |
10 |
Correct |
2 ms |
900 KB |
Output is correct |
11 |
Incorrect |
1 ms |
900 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
900 KB |
Output isn't correct |
13 |
Incorrect |
2 ms |
900 KB |
Output isn't correct |
14 |
Incorrect |
2 ms |
900 KB |
Output isn't correct |
15 |
Incorrect |
2 ms |
900 KB |
Output isn't correct |
16 |
Incorrect |
3 ms |
900 KB |
Output isn't correct |
17 |
Incorrect |
3 ms |
900 KB |
Output isn't correct |
18 |
Incorrect |
3 ms |
900 KB |
Output isn't correct |
19 |
Incorrect |
10 ms |
900 KB |
Output isn't correct |
20 |
Incorrect |
10 ms |
1004 KB |
Output isn't correct |