1 条题解

  • 0
    @ 2023-9-10 15:20:44
    #include<bits/stdc++.h>
    #define db double
    using namespace std;
    const int N=1010,M=5010;
    struct E
    {
        int u,v,t;
    }edge[M];
    int n,m,a[N];
    int tot,hd[N],nxt[M*2],to[M*2],q[N*M*2],vis[N];
    bool inq[N];db dis[N],cost[M*2];
    void add(int u,int v,db w)
    {nxt[++tot]=hd[u],to[tot]=v,cost[tot]=w,hd[u]=tot;}
    bool spfa()
    {
        int hed=0,tail=1,nw;
        memset(inq,0,sizeof inq);
        memset(dis,0,sizeof dis);
        memset(vis,0,sizeof vis);
        for(int i=1;i<=n;i++)
        {
            inq[i]=vis[i]=1;
            q[++hed]=i;
        }
        while(hed>=tail)
        {
            nw=q[tail++];
            for(int i=hd[nw];i!=-1;i=nxt[i])
            {
                if(dis[to[i]]>dis[nw]+cost[i])
                {
                    dis[to[i]]=dis[nw]+cost[i];
                    if(!inq[to[i]])
                    {
                        q[++hed]=to[i];
                        ++vis[to[i]];
                        if(vis[to[i]]>n)return 1;
                    }
                    inq[to[i]]=1;
                }
            }
            inq[nw]=0;
        }
        return 0;
    }
    bool check(db x)
    {
        memset(hd,-1,sizeof hd),tot=-1;
        for(int i=1;i<=m;i++)
            add(edge[i].u,edge[i].v,(db)x*edge[i].t-a[edge[i].v]);
        return spfa(); 
    }
    int main()
    {
        int u,v,w;db l=0,r=0,mid,ans;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]),r+=a[i];
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].t);
        for(;r-l>1e-4;)
        {
            mid=(l+r)/2;
            if(check(mid))l=mid;
            else r=mid-1e-3;
        }
        printf("%.2lf",l);
        return 0;
    }
    
    • 1

    信息

    ID
    1690
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    5
    已通过
    5
    上传者