BZOJ4898/5367 Apio2017商旅(分数规划+floyd)

  如果要在某点买入某物品并在另一点卖出,肯定是走其间最短路径。于是预处理任意两点间的收益和最短路径,连完边二分答案判负环即可,可以全程floyd。注意inf大小。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 110
#define M 10010
#define K 1010
#define inf 1000000001
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,m,k,a[N][K],b[N][K],d[N][N],v[N][N],t;
ll c[N][N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj5367.in","r",stdin);
    freopen("bzoj5367.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read(),m=read(),k=read();
    for (int i=1;i<=n;i++)
    {
        for (int j=1;j<=k;j++)
        a[i][j]=read(),b[i][j]=read();
    }
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        d[i][j]=inf;
    for (int i=1;i<=m;i++)
    {
        int x=read(),y=read(),z=read();
        d[x][y]=min(d[x][y],z);
    }
    for (int k=1;k<=n;k++)
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
            for (int x=1;x<=k;x++)
            if (~b[j][x]&&~a[i][x]) v[i][j]=max(v[i][j],b[j][x]-a[i][x]);
    int l=0,r=inf,ans=0;
    while (l<=r)
    {
        int mid=l+r>>1;
        for (int i=1;i<=n;i++)
            for (int j=1;j<=n;j++)
            c[i][j]=1ll*d[i][j]*mid-v[i][j];
        for (int k=1;k<=n;k++)
            for (int i=1;i<=n;i++)
                for (int j=1;j<=n;j++)
                c[i][j]=min(c[i][j],c[i][k]+c[k][j]);
        bool flag=0;for (int i=1;i<=n;i++) if (c[i][i]<=0) {flag=1;break;}
        if (flag) l=mid+1,ans=mid;
        else r=mid-1; 
    }
    cout<<ans;
    return 0;
}

 

posted @ 2018-11-30 02:35  Gloid  阅读(129)  评论(0编辑  收藏  举报