1. 程式人生 > >BZOJ4898/5367 Apio2017商旅(分數規劃+floyd)

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; }