1486: [HNOI2009]最小圈
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2005 Solved: 931
[ Submit][ Status][ Discuss]
Description
Input
Output
Sample Input
1 2 5
2 3 5
3 1 5
2 4 3
4 1 3
Sample Output
3.66666667
解题思路:01分数规划
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int n,m,len,flag;
int mark[3005],h[3005];
double w[11000],lg[11000],dis[3005];
int to[11000],next[11000];
inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0' || y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0' && y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;
}
void insert(int x,int y)
{
++len; to[len]=y; next[len]=h[x]; h[x]=len;
}
void spfa(int now)
{
mark[now]=1;
int u=h[now];
while (u!=0)
{
if (dis[to[u]]>dis[now]+lg[u])
{
if (mark[to[u]]==1)
{
flag=1; return;
}
dis[to[u]]=dis[now]+lg[u];
spfa(to[u]);
}
u=next[u];
}
mark[now]=0;
}
bool jud()
{
for (int i=1;i<=n;++i) dis[i]=mark[i]=0;
flag=0;
for (int i=1;i<=n;++i)
{
spfa(i); if (flag==1) return true;
}
return false;
}
int main()
{
len=0;
n=read(); m=read();
for (int i=1;i<=m;++i)
{
int x,y; x=read(); y=read();
double z; scanf("%lf",&z);
insert(x,y);
w[i]=z;
}
double l=-10000000,r=10000000;
while (r-l>1e-9)
{
double mid=(l+r)/2;
for (int i=1;i<=m;++i)
{
lg[i]=w[i]-mid;
}
if (jud()) r=mid;
else l=mid;
}
printf("%.8lf",r);
}