# 洛谷4926 [1007]倍杀测量者

## 差分约束

Posted by yjjr's blog on October 12, 2018

# 题目

## 输入输出样例

### 输入样例#1

3 5 1
1 2 1 2
1 3 2 2
1 3 1 4
2 1 2 2
2 1 3 4
1 1


### 输出样例#1

-1


### 输入样例#2

3 2 3
1 2 1 10
2 2 3 6
1 1
2 6
3 9


### 输出样例#2

3.9999993984


# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define mid ((l+r)/2)
#define eps 1e-15
#define v e[i].to
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e5;
struct node{ll o,x,y;double z;}p[maxn];
struct edge{int to,next;double w;}e[maxn<<1];
int last[maxn],num=0,cnt=0;bool flag,vis[maxn];
double dis[maxn],b[maxn];ll n,m,s,c,a[maxn];
void insert(int x,int y,double z){e[++cnt]=(edge){y,last[x],z};last[x]=cnt;}
void dfs(ll x,double k){
if(vis[x]){if(k/dis[x]>eps+1)flag=1;return;}
vis[x]=1;dis[x]=k;
reg(x){
dfs(v,k*e[i].w);
if(flag)return;
}
vis[x]=0;
}
int main()
{
s=n+1;
bool OK=0;double l=eps,r=10.0-eps;
while(num<=100){
num++;flag=false;
mem(last,0);mem(vis,0);mem(dis,0);
rep(i,1,c){insert(s,a[i],b[i]);insert(a[i],s,1.0/b[i]);}
rep(i,1,m)
if(!p[i].o)insert(p[i].y,p[i].x,p[i].z-mid);
else insert(p[i].y,p[i].x,1.0/(p[i].z+mid));
dfs(s,1);
if(flag)l=mid,OK=1;else r=mid;
}
if(OK)printf("%.6lf",l);else puts("-1");
return 0;
}