#include <bits/stdc++.h>
using namespace std;
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};
}
}
int 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<1000; 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?
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
5024 KB |
Output is correct |
2 |
Correct |
6 ms |
4948 KB |
Output is correct |
3 |
Correct |
7 ms |
5028 KB |
Output is correct |
4 |
Correct |
10 ms |
5036 KB |
Output is correct |
5 |
Correct |
25 ms |
4948 KB |
Output is correct |
6 |
Correct |
35 ms |
5048 KB |
Output is correct |
7 |
Correct |
79 ms |
5180 KB |
Output is correct |
8 |
Correct |
180 ms |
5748 KB |
Output is correct |
9 |
Correct |
5 ms |
4948 KB |
Output is correct |
10 |
Correct |
7 ms |
4956 KB |
Output is correct |
11 |
Correct |
7 ms |
5024 KB |
Output is correct |
12 |
Correct |
11 ms |
5028 KB |
Output is correct |
13 |
Correct |
10 ms |
4948 KB |
Output is correct |
14 |
Correct |
34 ms |
5076 KB |
Output is correct |
15 |
Correct |
40 ms |
4960 KB |
Output is correct |
16 |
Correct |
110 ms |
5204 KB |
Output is correct |
17 |
Correct |
92 ms |
5204 KB |
Output is correct |
18 |
Correct |
108 ms |
5212 KB |
Output is correct |
19 |
Correct |
174 ms |
5716 KB |
Output is correct |
20 |
Incorrect |
200 ms |
5752 KB |
Output isn't correct |