#include using namespace std; #include namespace mp = boost::multiprecision; using Bint = mp::cpp_int; //約分を毎回しない template struct yuri { T num, den; yuri() : num(0), den(1) {} yuri(T a) : num(a), den(1) {} yuri(int a) : num(a), den(1) {} yuri(long long a) : num(a), den(1) {} yuri(T a, T b) : num(a), den(b) {} void safe(){ if(num < 0) den *= -1, num *= -1; T v = gcd(num, den); num /= v, den /= v; } yuri& operator++() { num += den; return *this; } yuri& operator--() { num -= den; return *this; } yuri& operator+=(const yuri& rhs) { num *= rhs.den; num += rhs.num * den; den *= rhs.den; return *this; } yuri& operator-=(const yuri& rhs) { num *= rhs.den; num -= rhs.num * den; den *= rhs.den; return *this; } yuri& operator*=(const yuri& rhs) { num *= rhs.num; den *= rhs.den; return *this; } yuri& operator/=(const yuri& rhs) { num *= rhs.den; den *= rhs.num; return *this ; } yuri operator+() const { return *this; } yuri operator-() const { return yuri() - *this; } friend yuri operator+(const yuri lhs, const yuri rhs) { return yuri(lhs) += rhs; } friend yuri operator-(const yuri& lhs, const yuri& rhs) { return yuri(lhs) -= rhs; } friend yuri operator*(const yuri& lhs, const yuri& rhs) { return yuri(lhs) *= rhs; } friend yuri operator/(const yuri& lhs, const yuri& rhs) { return yuri(lhs) /= rhs; } friend bool operator==(const yuri& lhs, const yuri& rhs) { return (lhs.num * rhs.den == rhs.num * lhs.den); } friend bool operator!=(const yuri& lhs, const yuri& rhs) { return (lhs.num * rhs.den != rhs.num * lhs.den); } friend bool operator<(const yuri& lhs, const yuri& rhs) { return (lhs.num * rhs.den < rhs.num * lhs.den); } friend ostream& operator << (ostream &os, const yuri rhs) noexcept { return os << (rhs.num / rhs.den); } }; int main(){ ios::sync_with_stdio(false); cin.tie(0); static array,300>,300> A{}; for(int y = 0; y < 300; y++){ A[y].fill(yuri(1 << 30, 1)); } int n, m; cin >> n >> m; for(int i = 0; i < m; i++){ int u, v, a, b; cin >> u >> v >> a >> b; u--, v--; A[u][v] = min(A[u][v], yuri(a, b)); A[v][u] = min(A[v][u], yuri(a, b)); } for(int i = 0; i < n; i++) A[i][i] = yuri(0, 1); for(int k = 0; k < n; k++){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ auto v = A[i][k] + A[k][j]; if(v < A[i][j]){ A[i][j] = v; } } } } for(int i = 1; i < n; i++){ A[0][i].safe(); cout << A[0][i].num << ' ' << A[0][i].den << '\n'; } }