#include<bits/stdc++.h>
using namespace std;
#define FOR(i, x, y) for(int i = (x); i < (y); ++i)
#define REP(i, x, y) for(int i = (x); i <= (y); ++i)
#define PB push_back
#define MP make_pair
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
const int maxn = 255;
const int maxm = 10005;
int n, m;
int vis[maxn];
class Point{
public:
int x, y;
Point(){}
Point(int x_, int y_): x(x_), y(y_){}
inline bool operator == (const Point &oth)const{ return (x == oth.x && y == oth.y); }
inline int operator * (const Point &oth)const{ return x * oth.x + y * oth.y; }
inline Point operator * (const int &oth)const{ return Point(x * oth, y * oth); }
inline Point& operator += (const Point &oth){ x += oth.x, y += oth.y; return *this; }
inline Point& operator -= (const Point &oth){ x -= oth.x, y -= oth.y; return *this; }
inline Point operator - (const Point &oth)const{ Point ret = *this; ret -= oth; return ret; }
};
class Edge{
public:
int u, v;
Point val;
Edge(){}
Edge(int u_, int v_, Point val_): u(u_), v(v_), val(val_){}
} ed[maxm];
class Dsu{
private:
inline int find(int x){ return (x == fa[x]) ? x : (fa[x] = find(fa[x])); }
public:
int fa[maxn];
inline void init(){ FOR(i, 0, n) fa[i] = i; return; }
inline bool merge(int x, int y){
x = find(x), y = find(y);
if(x == y)
return false;
fa[x] = y;
return true;
}
} dsu;
inline Point kruscal(Point w){
vector<pii> vec;
FOR(i, 0, m)
vec.PB(MP(w * ed[i].val, i));
sort(vec.begin(), vec.end());
dsu.init();
Point ret(0, 0);
memset(vis, 0, sizeof(vis));
// printf("w = %d %d\n", w.x, w.y);
FOR(i, 0, vec.size()){
int id = vec[i].snd;
// printf("%d %d %d %d %d\n", vec[i].fst, ed[id].u, ed[i].v, ed[id].val.x, ed[id].val.y);
ret += ed[id].val * (vis[id] = dsu.merge(ed[id].u, ed[id].v));
}
return ret;
}
inline Point solve(Point A, Point B){
// printf("A = (%d %d) B = (%d %d)\n", A.x, A.y, B.x, B.y);
Point W = A - B; swap(W.x, W.y); W.x = -W.x;
Point C = kruscal(W);
Point P = ((C == A || C == B) ? A : solve(A, C)), Q = ((C == A || C == B) ? B : solve(C, B));
return (1ll * P.x * P.y < 1ll * Q.x * Q.y ? P : Q);
}
int main(){
scanf("%d%d", &n, &m);
FOR(i, 0, m){
int u, v, t, c; scanf("%d%d%d%d", &u, &v, &t, &c);
ed[i] = Edge(u, v, Point(t, c));
}
Point A = kruscal(Point(0, 1));
Point B = kruscal(Point(1, 0));
Point C = solve(A, B);
kruscal(Point(C.y, C.x));
printf("%d %d\n", C.x, C.y);
FOR(i, 0, m) if(vis[i])
printf("%d %d\n", ed[i].u, ed[i].v);
return 0;
}
Compilation message
timeismoney.cpp: In function 'Point kruscal(Point)':
timeismoney.cpp:5:41: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define FOR(i, x, y) for(int i = (x); i < (y); ++i)
| ^
timeismoney.cpp:69:2: note: in expansion of macro 'FOR'
69 | FOR(i, 0, vec.size()){
| ^~~
timeismoney.cpp: In function 'int main()':
timeismoney.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
timeismoney.cpp:88:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
88 | int u, v, t, c; scanf("%d%d%d%d", &u, &v, &t, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
8 |
Runtime error |
7 ms |
1052 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
19 |
Runtime error |
6 ms |
1052 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |
20 |
Runtime error |
6 ms |
1072 KB |
Execution killed with signal 6 (could be triggered by violating memory limits) |