You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.3 KiB
C++

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int inf = 0x3f3f3f3f, N = 10005, M = 100005;
int adj[N], nxt[M], to[M], cap[M], ecnt, idx;
double dis[N], cost[M];
inline void addEdge_impl_(int f, int t, int c, double w)
{
nxt[ecnt] = adj[f];
adj[f] = ecnt;
to[ecnt] = t;
cap[ecnt] = c;
cost[ecnt] = w;
ecnt++;
}
inline void addEdge(int f, int t, int c, double w)
{
addEdge_impl_(f, t, c, w);
addEdge_impl_(t, f, 0, -w);
}
int END;
bool bfs(int S)
{
memset(dis, 0x7f, sizeof(dis)), idx++;
static int que[N << 1];
static bool inq[N];
int len = 0;
dis[que[len++] = S] = 0;
for (int i = 0; i < len; inq[que[i++]] = false)
for (int e = adj[que[i]]; ~e; e = nxt[e])
if (cap[e] && (dis[to[e]] - dis[que[i]] - cost[e])>1e-8)
{
dis[to[e]] = dis[que[i]] + cost[e];
if (!inq[to[e]])
que[len++] = to[e];
}
return dis[END] < inf;
}
int dfs(int x, int rest)
{
static int vis[N];
if (x == END)
return rest;
vis[x] = idx;
int flow = 0;
for (int e = adj[x], curFlow; rest && ~e; e = nxt[e])
if (vis[to[e]] != idx && cap[e] && abs(dis[to[e]] - dis[x] - cost[e])<1e-8)
if (bool(curFlow = dfs(to[e], min(cap[e], rest))))
rest -= curFlow, flow += curFlow, cap[e] -= curFlow,
cap[e ^ 1] += curFlow;
return flow;
}
int main()
{
int T,n,m;
scanf("%d",&T);
while(T--)
{
memset(adj, -1, sizeof(adj));
ecnt=0;
scanf("%d%d",&n,&m);
END=n+1;
for(int i=1,a,b,c; i<=n; i++)
{
scanf("%d%d",&a,&b),c=a-b;
if(c>0)
addEdge(0,i,c,0);
if(c<0)
addEdge(i,n+1,-c,0);
}
double p;
for(int i=0,u,v,f; i<m; i++)
{
scanf("%d%d%d%lf",&u,&v,&f,&p);
if(f>0)
addEdge(u,v,1,0);
if(f>1)
addEdge(u,v,f-1,-log(1-p));
}
double ans=0;
while (bfs(0))
ans += dis[n+1] * dfs(0, inf);
printf("%.2f\n",1-exp(-ans));
}
return 0;
}