思路:取log化乘積爲加和。
#include <bits/stdc++.h>
using namespace std;
typedef int lint;
typedef long long LL;
struct EDGE {
int from, to, next, cap; // 如果需要修改 cost爲LL
double cost;
};
namespace MFMC {
const static int maxn = 100011;
const static int maxm = 500005;
const static double eps = 1e-8;
const static double inf = 1e10;
EDGE edge[maxm];
int tot, he[maxn], n;
void init(int _n) {
tot = 0;
n = _n + 1;
memset(he, -1, n * sizeof(int));
}
void Add(int u, int v, int cap,double cost) { // 如果需要修改 cost爲LL
edge[tot] = EDGE{u, v, he[u],cap,cost};
he[u] = tot++;
}
void add(int u, int v, int cap,double cost) { // 如果需要修改 cost爲LL
Add(u, v, cap,cost);
Add(v, u, 0,-cost);
}
//O(VE)
//record_e[i]是fa[i]->i的邊的編號
template<typename DT>
void spfa(int s, DT dist[], int rec[]) {
queue<int> q;
static bool inq[maxn];
for( int i = 0;i <= n;i++ ){
dist[i] = inf;
}
memset(inq, 0, n * sizeof(bool));
memset(rec, -1, n * sizeof(int));
q.push(s);
dist[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
inq[u] = 0;
for (int e = he[u]; ~e; e = edge[e].next) {
if (0 == edge[e].cap)
continue;
int v = edge[e].to;
if (dist[v] > dist[u] + edge[e].cost + eps ) {
dist[v] = dist[u] + edge[e].cost;
rec[v] = e;
if (!inq[v]) {
q.push(v);
inq[v] = 1;
}
}
}
}
}
template<typename DT>
void dijkstra(int s, DT dist[], int rec[]) {
priority_queue<pair<DT, int> > q;//-dist, vertex
for( int i = 0;i <= n;i++ ) dist[i] = inf;
memset(rec, -1, n * sizeof(int));
dist[s] = 0;
q.push(make_pair(0, s));
while (!q.empty()) {
s = q.top().second;
DT c = -q.top().first;
q.pop();
if (fabs(c - dist[s]) > eps ) continue;
for (int e = he[s]; ~e; e = edge[e].next) {
if (0 == edge[e].cap) continue;
int v = edge[e].to;
if (dist[v] > c + edge[e].cost+eps) {
dist[v] = c + edge[e].cost;
rec[v] = e;
q.push(make_pair(-dist[v], v));
}
}
}
}
//Need dijkstra_GRAPH_EDGES_PQ
//O(FE log(E)),F is the maximum flow
template<typename FT, typename CT>
void mfmc(int s, int t, FT &maxflow, CT &mincost) {
static CT dist[maxn];
static int rec_e[maxn];
maxflow = mincost = 0;
CT realdist = 0; //real distance from s to t
bool first = true;
while (1) {
if (first) {
spfa( s, dist, rec_e);
first = false;
} else {
dijkstra( s, dist, rec_e);
}
if (fabs(inf - dist[t]) < eps)
break;
FT minF = numeric_limits<FT>::max();
for (int e = rec_e[t]; ~e; e = rec_e[edge[e].from])
minF = min(minF, (FT) edge[e].cap);
maxflow += minF;
realdist += dist[t];
mincost += minF * realdist;
for (int e = rec_e[t]; ~e; e = rec_e[edge[e].from]) {
edge[e].cap -= minF;
edge[e ^ 1].cap += minF;
}
for (int e = 0; e < tot; ++e) {
EDGE &ed = edge[e];
ed.cost += dist[ed.from] - dist[ed.to];
}
}
}
};
const int maxn = 205;
const int inf = 0x3f3f3f3f;
int s[maxn],b[maxn];
int main(){
int ca;
scanf("%d",&ca);
while(ca--){
int n,m;
scanf("%d%d",&n,&m);
int S = 0,T = 2*n+1;
MFMC::init(T);
for( int i = 1;i <= n;i++ ){
scanf("%d%d",&s[i],&b[i]);
MFMC::add( i,i+n,inf,0 );
MFMC::add( S,i,s[i],0 );
MFMC::add( i+n,T,b[i],0 );
}
double v;int x,y,c;
for( int i = 1;i <= m;i++ ){
scanf("%d%d%d%lf",&x,&y,&c,&v);
v = 1-v;
MFMC::add( x+n,y,1,0 );
MFMC::add( x+n,y,c-1,-log(v) );
}
int maxflow;double mincost;
MFMC::mfmc( S,T,maxflow,mincost );
double ans= 1 - exp( -mincost );
printf("%.2lf\n",ans);
}
return 0;
}