bzoj 1690: [Usaco2007 Dec] Cows' Travel [01 Score Planning + spfa]

Write the double in the add parameter as int, I am also Shi Lezhi...
First of all, this thing looks like a 01 score plan
, and then I will not prove why there is no 8-character ring, let's pretend he doesn't have it,
then set len ​​as the ring length
\[ ans \leq \frac{\sum_{i=1}^{len}f_i}{\sum_{i=1}^{len}t_i} \]
\[ ans*\sum_{i=1}^{len}t_i \leq \sum_{i=1}^{len}f_i \]
\[ \sum_{i=1}^{len}ans*t_i-f_i \leq 0 \]
Divide this ans, and assign the edge weight of each edge as \( ans*t_i-f_i \)fi is the starting point weight or the end point weight of the edge i can
then use the dfs version of spfa to find the positive ring to judge the feasibility of the current ans

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=1005,M=5005;
const double eps=1e-5;
int n,m,h[N],cnt,x[M],y[M];
double a[N],z[M],dis[N];
bool f,v[N];
struct qwe
{
    int ne,to;
    double va;
}e[M];
int read()
{
    int r=0,f=1;
    char p=getchar();
    while(p>'9'||p<'0')
    {
        if(p=='-')
            f=-1;
        p=getchar();
    }
    while(p>='0'&&p<='9')
    {
        r=r*10+p-48;
        p=getchar();
    }
    return r*f;
}
void add(int u,int v,double w)
{
    cnt++;
    e[cnt].ne=h[u];
    e[cnt].to=v;
    e[cnt].va=w;
    h[u]=cnt;
}
void spfa(int u)
{
    if(f)
        return;
    v[u]=1;
    for(int i=h[u];i&&!f;i=e[i].ne)
        if(dis[e[i].to]<dis[u]+e[i].va)
        {
            dis[e[i].to]=dis[u]+e[i].va;
            if(v[e[i].to])
            {
                f=1;
                return;
            }
            else
                spfa(e[i].to);
        }
    v[u]=0;
}
bool ok(double mid)
{
    cnt=0;f=0;
    memset(h,0,sizeof(h));
    memset(v,0,sizeof(v));
    memset(dis,0,sizeof(dis));
    for(int i=1;i<=m;i++)
        add(x[i],y[i],a[x[i]]-mid*z[i]);
    for(int i=1;i<=n;i++)
    {
        spfa(i);
        if(f)
            return 1;
    }
    return 0;
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    for(int i=1;i<=m;i++)
        x[i]=read(),y[i]=read(),z[i]=read();
    double l=0,r=1e6,ans=0;
    while(r-l>eps)
    {
        double mid=(l+r)/2.0;
        if(ok(mid))
            l=mid,ans=mid;
        else
            r=mid;
    }
    printf("%.2lf\n",ans);
    return 0;
}

Guess you like

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