#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; \
pvaspace=true;\
b << pva;\
}\
b << "\n";}
#define SZ(a) int(a.size())
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
//typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
//using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 2147483647;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
struct DSU{
vector<int> dsu;
void initDSU(int n){
dsu.resize(n + 1);
iota(iter(dsu), 0);
}
int findDSU(int a){
if(dsu[a] != a) dsu[a] = findDSU(dsu[a]);
return dsu[a];
}
void unionDSU(int a, int b){
a = findDSU(a); b = findDSU(b);
dsu[b] = a;
}
};
#define X F
#define Y S
pll operator-(pll p1, pll p2){
return mp(p1.X - p2.X, p1.Y - p2.Y);
}
ll cross(pll p1, pll p2){
return p1.X * p2.Y - p1.Y * p2.X;
}
int main(){
StarBurstStream;
int n, m;
cin >> n >> m;
vector<pii> e(m);
vector<ll> wt(m), wc(m);
for(int i = 0; i < m; i++){
int u, v, t, c;
cin >> u >> v >> t >> c;
u++; v++;
e[i] = mp(u, v);
wt[i] = t;
wc[i] = c;
}
ll anst = 1e9, ansc = 1e9;
ll answ1 = -1, answ2 = -1, ansrev = -1;
auto calc = [&](ll w1, ll w2, int rev, bool print = false){
vector<int> id(m);
iota(iter(id), 0);
sort(iter(id), [&](int x, int y){ return w1 * wt[x] + w2 * wc[x] < w1 * wt[y] + w2 * wc[y]; });
if(rev) reverse(iter(id));
DSU dsu;
dsu.initDSU(n);
ll tt = 0, tc = 0;
vector<int> ok;
for(int i : id){
int u, v;
tie(u, v) = e[i];
if(dsu.findDSU(u) == dsu.findDSU(v)) continue;
ok.eb(i);
dsu.unionDSU(u, v);
tt += wt[i]; tc += wc[i];
}
if(anst * ansc > tt * tc){
anst = tt; ansc = tc;
answ1 = w1; answ2 = w2; ansrev = rev;
}
if(print) for(int i : ok) cout << e[i].F - 1 << " " << e[i].S - 1 << "\n";
return mp(tt, tc);
};
function<void(pll, pll)> solve = [&](pll p1, pll p2){
//cerr << "solve " << p1 << " " << p2 << "\n";
if(p1 == p2) return;
ll dx = p2.X - p1.X;
ll dy = p2.Y - p1.Y;
pll p = calc(dy, -dx, 1);
if(cross(p - p1, p2 - p1) <= 0) return;
//cerr << "done " << p << "\n";
solve(p1, p);
solve(p, p2);
};
pll p1 = calc(1, 0, 0);
pll p2 = calc(1, 0, 1);
solve(p1, p2);
cout << anst << " " << ansc << "\n";
calc(answ1, answ2, ansrev, true);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
6 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
13 ms |
332 KB |
Output is correct |
15 |
Correct |
8 ms |
212 KB |
Output is correct |
16 |
Correct |
223 ms |
352 KB |
Output is correct |
17 |
Correct |
212 ms |
340 KB |
Output is correct |
18 |
Correct |
215 ms |
356 KB |
Output is correct |
19 |
Correct |
1837 ms |
596 KB |
Output is correct |
20 |
Correct |
1914 ms |
580 KB |
Output is correct |