#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int, int> pii;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define sz(x) (int)x.size()
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e5+5, INF = 1e9;
int n, m;
struct State{
int to, time, cost, from;
};
vector<State> arr[MAXN], output;
pii edges[MAXN];
pair<ll, ll> curMin = {60000, 60000};
void prims(int a, int b){
auto cmp = [&](State& x, State& y){
return x.time * a + x.cost * b > y.time * a + y.cost * b;
};
priority_queue<State, vector<State>, decltype(cmp)> pq(cmp);
vector<int> cost(n, INF);
vector<bool> used(n);
vector<State> mst;
cost[0] = 0;
pq.push({0, 0, 0, -1});
while (!pq.empty()){
State cur = pq.top();
pq.pop();
if (used[cur.to]){
continue;
}
used[cur.to] = true;
mst.push_back(cur);
for (State x : arr[cur.to]){
if (x.time * a + x.cost * b < cost[x.to] && !used[x.to]){
cost[x.to] = x.time * a + x.cost * b;
pq.push(x);
}
}
}
ll time = 0, money = 0;
for (State x : mst){
time += x.time;
money += x.cost;
}
if (time * money < curMin.first * curMin.second){
swap(output, mst);
curMin = {time, money};
}
}
signed main(){
cin.tie(0); ios_base::sync_with_stdio(0);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
cin >> n >> m;
for (int i=0; i<m; i++){
int a, b, t, c;
cin >> a >> b >> t >> c;
edges[i] = {t, c};
arr[a].push_back({b, t, c, a});
arr[b].push_back({a, t, c, b});
}
for (int i=1; i<2000; i++){
prims(uid(1, 51000), uid(1, 51000));
}
cout << curMin.first << " " << curMin.second << "\n";
for (int i=1; i<sz(output); i++){
cout << output[i].from << " " << output[i].to << "\n";
}
}
/**
* Debugging checklist:
* - Reset everything after each TC
* - Integer overflow, index overflow
* - Special cases?
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
5076 KB |
Output is correct |
2 |
Correct |
8 ms |
4948 KB |
Output is correct |
3 |
Correct |
10 ms |
4948 KB |
Output is correct |
4 |
Correct |
13 ms |
4948 KB |
Output is correct |
5 |
Correct |
49 ms |
5084 KB |
Output is correct |
6 |
Correct |
61 ms |
5080 KB |
Output is correct |
7 |
Correct |
142 ms |
5300 KB |
Output is correct |
8 |
Correct |
389 ms |
6136 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
8 ms |
4948 KB |
Output is correct |
11 |
Correct |
10 ms |
4912 KB |
Output is correct |
12 |
Correct |
15 ms |
5044 KB |
Output is correct |
13 |
Correct |
21 ms |
4948 KB |
Output is correct |
14 |
Correct |
57 ms |
5076 KB |
Output is correct |
15 |
Correct |
76 ms |
5080 KB |
Output is correct |
16 |
Correct |
216 ms |
5332 KB |
Output is correct |
17 |
Correct |
217 ms |
5328 KB |
Output is correct |
18 |
Correct |
198 ms |
5312 KB |
Output is correct |
19 |
Correct |
404 ms |
6124 KB |
Output is correct |
20 |
Correct |
404 ms |
6192 KB |
Output is correct |