luogu3778/bzoj4898 Hotel (floyd+ score planning +SPFA)

Source: Internet
Author: User

First Floyd to find out the shortest distance between every two points, and then find out from a point to buy to a point to sell the maximum benefit

The problem is to find the one with the greatest ratio of the ring

So do score planning, two points out the answer R, turn Benquan into W[i]-r*l[i], and then do SPFA sentence positive ring on the line

(originally want to lazy use Floyd sentence positive ring, result T)

1#include <bits/stdc++.h>2 #definePA pair<int,int>3 #defineCLR (a,x) memset (A,x,sizeof (a))4 using namespacestd;5typedefLong Longll;6 Const intmaxn= the, maxm=10010, maxk=1010;7 Constll inf=1e15;8 9 Inline LL Rd () {Tenll x=0;CharC=getchar ();intneg=1; One      while(c<'0'|| C>'9'){if(c=='-') neg=-1; c=GetChar ();} A      while(c>='0'&&c<='9') x=x*Ten+c-'0', c=GetChar (); -     returnx*neg; - } the  - intW[MAXN][MAXN]; - intSELL[MAXN][MAXK],BUY[MAXN][MAXK]; - intN,M,K,CNT[MAXN]; + ll DIS[MAXN][MAXN],D[MAXN][MAXN],DD[MAXN]; - BOOLINQ[MAXN]; +queue<int>Q; A  at BOOLSPFA (ints) { -      while(!q.empty ()) Q.pop (); -dd[s]=0; Q.push (s); cnt[s]=1; -      while(!Q.empty ()) { -         intP=q.front (); inq[p]=0; -         //printf ("%d%d%d\n", p,cnt[p],dd[p]); in Q.pop (); -          for(intb=1; b<=n;b++){ to             if(D[p][b]==-inf)Continue; +             if(dd[b]<=dd[p]+D[p][b]) { -dd[b]=dd[p]+D[p][b]; the                 if(Inq[b])Continue; *                 if(++cnt[b]>n)return 1; $ Q.push (b);Panax Notoginsenginq[b]=1; -             } the         } +}return 0; A } the  +InlineBOOLjudge (ll R) { -     //printf ("%lld:\n", r); $      for(intI=1; i<=n;i++){ $          for(intj=1; j<=n;j++) -D[i][j]= (dis[i][j]==-1)?-inf:w[i][j]-r*Dis[i][j]; -     } the     BOOLRe=0; -CLR (CNT,0); CLR (INQ,0);Wuyi      for(intI=1; i<=n;i++) dd[i]=-inf; the      for(intI=1; i<=n&&!re;i++){ -         if(!cnt[i]) re|=SPFA (i); Wu     } -     returnre; About } $  - intMain () { -     //freopen ("", "R", stdin); -     inti,j,k; AN=rd (), M=rd (), k=Rd (); +      for(i=1; i<=n;i++){ the          for(j=1; j<=k;j++){ -Buy[i][j]=rd (), sell[i][j]=Rd (); $         } the     } the      for(i=1; i<=n;i++){ the          for(j=1; j<=n;j++){ the             if(I==J)Continue; -              for(k=1; k<=k;k++){ in                 if(sell[j][k]==-1|| buy[i][k]==-1)Continue; theW[i][j]=max (w[i][j],sell[j][k]-buy[i][k]); the             } About         } the     } theCLR (dis,-1); the      for(i=1; i<=m;i++){ +         intA=rd (), B=rd (), c=Rd (); -dis[a][b]=C; the     }Bayi      for(i=1; i<=n;i++){ the          for(j=1; j<=n;j++){ the             if(dis[j][i]==-1)Continue; -              for(k=1; k<=n;k++){ -                 if(dis[i][k]==-1)Continue; the                 if(dis[j][k]==-1|| dis[j][k]>dis[j][i]+Dis[i][k]) thedis[j][k]=dis[j][i]+Dis[i][k]; the             } the         } -     } the     //for (i=1;i<=n;i++) for (j=1;j<=n;j++) printf ("%d-%d,%lld,%lld\n", I,j,dis[i][j],w[i][j]); the      thell l=0, r=inf,ans=0;94      while(l<=S) { the         intM=l+r>>1; the         if(judge (m)) ans=m,l=m+1; the         Elser=m-1;98     } Aboutprintf"%lld\n", ans); -     return 0;101}

luogu3778/bzoj4898 Hotel (floyd+ score planning +SPFA)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.