답안 #43719

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
43719 2018-03-21T11:07:25 Z ssnsarang2023 시간이 돈 (balkan11_timeismoney) C++14
100 / 100
708 ms 1692 KB
#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, t.first - s.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 356 KB Output is correct
3 Correct 2 ms 488 KB Output is correct
4 Correct 2 ms 488 KB Output is correct
5 Correct 3 ms 488 KB Output is correct
6 Correct 2 ms 520 KB Output is correct
7 Correct 4 ms 644 KB Output is correct
8 Correct 8 ms 1144 KB Output is correct
9 Correct 2 ms 1144 KB Output is correct
10 Correct 2 ms 1144 KB Output is correct
11 Correct 2 ms 1144 KB Output is correct
12 Correct 2 ms 1144 KB Output is correct
13 Correct 2 ms 1144 KB Output is correct
14 Correct 6 ms 1144 KB Output is correct
15 Correct 5 ms 1144 KB Output is correct
16 Correct 76 ms 1144 KB Output is correct
17 Correct 79 ms 1144 KB Output is correct
18 Correct 84 ms 1144 KB Output is correct
19 Correct 591 ms 1552 KB Output is correct
20 Correct 708 ms 1692 KB Output is correct