Problem: Example

Input

2 1
1 2
1 2 1 1


Output

1/2

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define int long long

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

//最大流量がint,頂点数がMAX_N
//Dinic<int> dn(MAX_N);
//適宜add_edge,max_flowを用いる
template<typename T> class Dinic {
public:
    struct edge{
        int to;
        T cap;
        int rev;
    };
    vector<vector<edge> > G;
    vector<int> level,iter;
    int n;
    Dinic(int node_size){
        n = node_size;
        G.resize(node_size),level.resize(node_size),iter.resize(node_size);
    }
    //辺を張る
    void add_edge(int from,int to,T cap)
    {
    	G[from].push_back((edge){to,cap,(int)G[to].size()});
    	G[to].push_back((edge){from,(T)0,(int)G[from].size()-1});
    }
    void bfs(int s)
    {
    	fill(level.begin(),level.end(),-1);
    	queue<int> que;
    	level[s] = 0;
    	que.push(s);
    	while(!que.empty()){
    		int v = que.front();
    		que.pop();
    		rep(i,G[v].size()){
    			edge &e = G[v][i];
    			if(e.cap > 0 && level[e.to] < 0){
    				level[e.to] = level[v] + 1;
    				que.push(e.to);
    			}
    		}
    	}
    }
    T dfs(int v,int t,T f)
    {
    	if(v==t){
    		return f;
    	}
    	for(int &i = iter[v];i<(int)G[v].size();i++){
    		edge &e = G[v][i];
    		if(e.cap > 0 && level[v] < level[e.to]){
    			T d = dfs(e.to,t,min(f,e.cap));
    			if(d>0){
    				e.cap -= d;
    				G[e.to][e.rev].cap += d;
    				return d;
    			}
    		}
    	}
    	return 0;
    }
    //最大流を計算
    T max_flow(int s,int t)
    {
    	T flow = 0;
    	for(;;){
    		bfs(s);
    		if(level[t]<0){
    			return flow;
    		}
    		fill(iter.begin(),iter.end(),0);
    		T f;
    		while((f=dfs(s,t,numeric_limits<T>::max())) > 0){
    			flow += f;
    		}
    	}
    }
};

int gcd(int a,int b)
{
    if(a == 0 || b == 0){
        return max(a,b);
    }
    if(a % b == 0){
        return b;
    }else{
        return gcd(b,a%b);
    }
}

template<typename T> class min_cost_flow{
public:
    struct edge {
        int to,cap;
        T cost;
        int rev;
    };
	using pti = pair<T,int>;
    vector<vector<edge> > G;
	vector<T> h,dist;
	vector<int> prevv,preve;
	T inf;
    int V;
    min_cost_flow(int node_size){
        V = node_size;
		inf = numeric_limits<T>::max() / 100;
        G.resize(V), h.resize(V), dist.resize(V), prevv.resize(V), preve.resize(V);
	}
    void add_edge(int from, int to, int cap, T cost){
        G[from].push_back((edge){to, cap, cost, (int)G[to].size()});
        G[to].push_back((edge){from, 0, -cost, (int)G[from].size() - 1});
    }
    pair<T,T> solve(int s,int t,int f,int M){
        T res = 0;
        int nw = M*M;
		fill(h.begin(),h.end(),0);
        while(f > 0){
            priority_queue<pti,vector<pti>,greater<pti> > que;
            fill(dist.begin(),dist.end(),inf);
            dist[s] = 0;
            que.push(pti(0,s));
            while(!que.empty()){
                pti p = que.top();
                que.pop();
                int v = p.second;
                if(dist[v] < p.first){
                    continue;
                }
                rep(i,G[v].size()){
                    edge& e = G[v][i];
                    if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){
                        dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                        prevv[e.to] = v, preve[e.to] = i;
                        que.push(pti(dist[e.to], e.to));
                    }
                }
            }
            rep(i,V){
                h[i] += dist[i];
            }
            int d = f;
            for(int v = t; v != s; v = prevv[v]){
                d = min(d, G[prevv[v]][preve[v]].cap);
            }
            int ff = M-f;
            int p = M-ff-res*h[t];
            int q = h[t]*h[t]+1;
            if(p <= d*q){
                if(p <= 0){
                    return make_pair(nw,1);
                }else{
                    p *= p;
                    int g = gcd(p,q);
                    p /= g, q /= g;
                    return make_pair(q*(res*res+M*M+ff*ff-2*ff*M)-p,q);
                }
            }else{
                nw = (res+d*h[t])*(res+d*h[t])+(M-ff-d)*(M-ff-d);
            }
            f -= d;
            res += d * h[t];
            for(int v = t; v != s;v = prevv[v]){
                edge& e = G[prevv[v]][preve[v]];
                e.cap -= d;
                G[v][e.rev].cap += d;
            }
        }
        return make_pair(nw,1);
    }
};

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    Dinic<int> dn(n);
    min_cost_flow<int> mcf(n);
    int s,t;
    cin >> s >> t;
    --s,--t;
    rep(i,m){
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        dn.add_edge(a-1,b-1,c);
        mcf.add_edge(a-1,b-1,c,d);
    }
    int M = dn.max_flow(s,t);
    auto res = mcf.solve(s,t,M,M);
    cout << res.fi << "/" << res.se << "\n";
    return 0;
}