#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast")
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 0, n + 1)
#define pb push_back
#define pf push_front
#define eb emplace_back
#define f first
#define s second
#define lowbit(x) x&-x
#define ckmin(a, b) a = min(a, b)
#define ckmax(a, b) a = max(a, b)
#define int long long
const int INF = 1e9 + 7;
const int maxn = 300;
const int maxm = 1e4 + 5;
int n, m;
int par[maxn];
int root(int x){ return x == par[x] ? x : par[x] = root(par[x]);}
struct edge{
edge(){}
edge(int _x, int _y, int _t, int _c, int _w) : x(_x), y(_y), t(_t), c(_c), w(_w){}
int x, y;
int t, c;
int w;
bool operator < (const edge& other) const{
return w < other.w;
}
} e[maxm];
struct pt{
int x, y; // (T, C)
pt(){}
pt(int _x, int _y) : x(_x), y(_y){}
pt operator - (const pt b){
return pt(x - b.x, y - b.y);
}
int cross(const pt b){
return x * b.y - y * b.x;
}
} ans, A, B;
vector<pll> best;
pt kruskal(){
for(int i = 0; i < n; i++) par[i] = i;
sort(e, e + m);
pt ret = {0, 0};
vector<pll> cur;
for(int i = 0; i < m; i++){
int x = root(e[i].x), y = root(e[i].y);
if(x == y) continue;
cur.eb(e[i].x, e[i].y);
par[x] = y, ret.x += e[i].t, ret.y += e[i].c;
}
if(ret.x * ret.y < ans.x * ans.y) ans = ret, best = cur;
return ret;
}
void work(pt A, pt B){ // divide and conquer
for(int i = 0; i < m; i++) e[i].w = e[i].t * (A.y - B.y) + e[i].c * (B.x - A.x);
pt C = kruskal();
if((B - A).cross(C - A) >= 0) return;
work(A, C), work(C, B);
}
signed main(void){
fastio;
cin>>n>>m;
ans = {INF, 1};
for(int i = 0; i < m; i++){
int x, y, t, c;
cin>>x>>y>>t>>c;
e[i] = edge(x, y, t, c, 0);
}
for(int i = 0; i < m; i++) e[i].w = e[i].t;
A = kruskal();
for(int i = 0; i < m; i++) e[i].w = e[i].c;
B = kruskal();
work(A, B);
cout<<ans.x<<" "<<ans.y<<"\n";
for(auto [x, y] : best) cout<<x<<" "<<y<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
4 ms |
992 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
352 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
4 ms |
496 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
52 ms |
344 KB |
Output is correct |
17 |
Correct |
54 ms |
348 KB |
Output is correct |
18 |
Correct |
51 ms |
348 KB |
Output is correct |
19 |
Correct |
434 ms |
976 KB |
Output is correct |
20 |
Correct |
448 ms |
1108 KB |
Output is correct |