#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;
vector<int> anse;
auto calc = [&](function<bool(int, int)> comp){
vector<int> id(m);
iota(iter(id), 0);
sort(iter(id), comp);
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;
anse = ok;
}
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([&](int x, int y){
return dy * wt[x] - dx * wc[x] > dy * wt[y] - dx * wc[y];
});
if(cross(p - p1, p2 - p1) <= 0) return;
//cerr << "done " << p << "\n";
solve(p1, p);
solve(p, p2);
};
pll p1 = calc([&](int x, int y){ return wt[x] < wt[y]; });
pll p2 = calc([&](int x, int y){ return wt[x] > wt[y]; });
solve(p1, p2);
cout << anst << " " << ansc << "\n";
for(int i : anse){
cout << e[i].F - 1 << " " << e[i].S - 1 << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
668 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 |
25 ms |
340 KB |
Output is correct |
15 |
Correct |
15 ms |
340 KB |
Output is correct |
16 |
Correct |
447 ms |
392 KB |
Output is correct |
17 |
Correct |
435 ms |
392 KB |
Output is correct |
18 |
Correct |
424 ms |
396 KB |
Output is correct |
19 |
Execution timed out |
2084 ms |
724 KB |
Time limit exceeded |
20 |
Execution timed out |
2079 ms |
724 KB |
Time limit exceeded |