# 洛谷3199 [hnoi2009]最小圈

## 分数规划+SPFA判负环

Posted by yjjr's blog on October 30, 2018

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

3.66666667

2 2
1 2 -2.9
2 1 -3.1

-3.00000000

# code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#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)
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;
}
#define inf 1e6
const int maxn=1e4+6;
int n,m,cnt=0,last[maxn],u[maxn],v[maxn],judge;
double w,dis[maxn];bool vis[maxn];
struct edge{int to,next;double v;}e[maxn];
void insert(int u,int v,double w){
e[++cnt]=(edge){v,last[u],w};last[u]=cnt;
}
void spfa(int x,double w){
vis[x]=1;
reg(x)
if(dis[x]+e[i].v-w<dis[e[i].to]){
if(judge||vis[e[i].to]){judge=1;break;}
dis[e[i].to]=dis[x]+e[i].v-w;
spfa(e[i].to,w);
}
vis[x]=0;
}
int main(){
double l=-inf,r=inf;
int T=66;
while(T--){
double mid=(l+r)/2;
mem(vis,0);mem(dis,0);judge=0;
rep(i,1,n){
spfa(i,mid);
if(judge)break;
}
if(judge)r=mid;else l=mid;
}
printf("%.8lf\n",l);
return 0;
}