「BZOJ1486」[HNOI2009] 最小圈
题解
分数规划,二分答案用dfs版的spfa判负环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#include<cstdio> #include<cmath> #include<ctime> #include<cstring> #include<iostream> #include<algorithm> #include<queue> #define ll long long #define pa pair<int,int> #define mod 1000000007 #define inf 100000000 using namespace std; int read() { int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x; } bool flag; int n,m,cnt; int last[3005]; int u[10005],v[10005]; double w[10005],dis[3005]; bool mark[3005]; struct edge{ int to,next;double v; }e[10005]; void insert(int u,int v,double w) { e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w; } void build(double mid) { memset(last,0,sizeof(last));cnt=0; for(int i=1;i<=m;i++) insert(u[i],v[i],w[i]-mid); } void spfa(int x) { mark[x]=1; for(int i=last[x];i;i=e[i].next) if(dis[x]+e[i].v<dis[e[i].to]) { if(mark[e[i].to]){flag=1;break;} else { dis[e[i].to]=dis[x]+e[i].v; spfa(e[i].to); } } mark[x]=0; } bool jud(double mid) { build(mid); memset(mark,0,sizeof(mark)); memset(dis,0,sizeof(dis)); flag=0; for(int i=1;i<=n;i++) { spfa(i);if(flag)return 1; } return 0; } int main() { n=read();m=read(); for(int i=1;i<=m;i++) { u[i]=read();v[i]=read();scanf("%lf",&w[i]); } double l=-10000000,r=10000000; int T=60; while(T--) { double mid=(l+r)/2; if(jud(mid)) r=mid; else l=mid; } printf("%.8lf",l); return 0; } |
Subscribe