BZOJ1486: [HNOI2009] Minimum circle - problem solving

https://www.lydsy.com/JudgeOnline/problem.php?id=1486

https://www.luogu.org/problemnew/show/P3199

The title is too ghostly to be sticky.

The only correct solution to this question is https://www.luogu.org/blog/user7868/solution-p3199 although I don't understand it.

Of course, for the question of AC, I abandoned my soul and wrote dfs-spfa, which ran very fast.

This kind of question is out of the pot...

Briefly talk about the method, divide the answer into two, subtract this answer from each edge and search for a negative ring. If there is one, the answer is legal, otherwise it is illegal.

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef double dl;
const dl INF=1e7;
const dl eps=1e-9;
const int N=3e3+5;
const int M=1e4+5;
struct node{
    int to,nxt;
    dl w;
}e[M];
int cnt,head[N],n,m,sum[N];
bool vis[N],ok;
dl dis[N];
queue<int>q;
inline void add(int u,int v,dl w){
    e[++cnt].to=v;e[cnt].w=w;e[cnt].nxt=head[u];head[u]=cnt;
}
void spfa(int u){
    vis[u]=1;
    for(int i=head[u];i&&!ok;i=e[i].nxt){
        int v=e[i].to;dl w=e[i].w;
        if(dis[v]>=dis[u]+w){
            dis[v]=dis[u]+w;
            if(vis[v]||ok){ok=1;return;}
            spfa (v);
        }
    }
    vis [u] = 0 ;
}
bool pan(dl delta){
    for(int i=1;i<=m;i++)e[i].w-=delta;
    for(int i=1;i<=n;i++)vis[i]=0,dis[i]=0;
    ok=0;
    for(int i=1;i<=n&&!ok;i++)
           spfa(i);
    for(int i=1;i<=m;i++)e[i].w+=delta;
    return ok;
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v;dl w;
        scanf("%d%d%lf",&u,&v,&w);
        add(u,v,w);
    }
    dl l=-INF,r=INF;
    while(fabs(r-l)>eps){
        dl mid=(l+r)/2;
        if(pan(mid))r=mid;
        else l=mid;
    }
    printf("%.8lf\n",l);
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+ Author of this article: luyouqi233. +

+Welcome to my blog: http://www.cnblogs.com/luyouqi233/  +

+++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325103240&siteId=291194637