[Explanations] Leyni car race

[Explanations] Leyni car race

HRBUST - 1404

Thinking problems? I was surprised to be hash out

This presupposition FIG topic such a state
\ [ans (i, j,
f) \] represents from i to j, a maximum use of the f vehicle Shortest

Transfer case, each vehicle to run their own a Floyd, and then into the like, transferred to enumerate the middle.

But you say \ (f \) great feeling to do this to die, but carefully think about the most change \ (\ n) vehicles will reach the end, so let \ (f \) to \ (n \) to take min just fine.

This method of analysis complexity is useful, there is a more typical example, a range of such question, interval length is \ (100 \) , asking whether \ (1e5 \) , but every time you ask only \ (O (n ^ 2) \) violence, how about this subtask? direct memory of ah! Total on \ (100 \ times 90 \) different intervals ah

So we get a \ (O (n ^ 5) \) practice, I thought I could too, but more than one set of data, pay out of the T

for(register int t=1;t<=m;++t)
     g[t].read(),g[t].gen();
for(register int t0=1;t0<=m;++t0)
     for(register int f=1;f<=n+2;++f)
          for(register int k=1;k<=n;++k)
               for(register int t=1;t<=n;++t)
                    for(register int i=1;i<=n;++i)
                         ans[t][i][f]=Min(ans[t][i][f],ans[t][i][f-1],ans[t][k][f-1]+g[t0][k][i],ans[t][k][f-1]+ans[k][i][1]);
for(register int t=1;t<=r;++t){
     register int t1=qr(),t2=qr(),t3=min(qr()+1,n+1);
     printf("%d\n",ans[t1][t2][t3]);
}

Code \ (G [] \) is defined by the structure I, which is a \ (E [MAXN] [MAXN] \) , \ (G [T] .gen () \) is run again floyd

Consider optimizing it, because we are minimum requirements, we lookChaotic transfer, Considering moving hands and feet in the third parameter Min function, in fact, for a determined \ (F \) , we find a minimum \ (g [t0] [k ] [i] \) like, we just find something to keep it get away.

In fact, I feel this shift on the code to do this thing, that \ (ans [t] [k] [f-1] + ANS [k] [i] [1] \) , where \ (ans [ k] [i] [1] \) actually recorded minimal \ (G [T0] [K] [I] \) . So we enumerate a few \ (t0 \) , the complexity becomes \ (O (n ^ 4) \)

This problem could have been thinking I can not make come, but d is why I wrote out? Because I found that the algorithm can be optimized by observing the behavior of the code. . . I used this technique has helped more than I took in a simulated game in a several percentage 23,333,333

No retirement depends on it

//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;  typedef long long ll;
inline int qr(){
      register int ret=0,f=0;
      register char c=getchar();
      while(c<48||c>57)f|=c==45,c=getchar();
      while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
      return f?-ret:ret;
}

const int maxn=51;
const int inf=0x3f3f3f3f;
int ans[maxn][maxn][53];
int n,m,r;
struct Graph{
      int e[maxn][maxn];
      inline void read(){
        for(register int t=1;t<=n;++t)
          for(register int i=1;i<=n;++i)
            e[t][i]=qr();
      }
      inline int* operator [](register int x){return e[x];}
      inline void gen(){
        for(register int k=1;k<=n;++k)
          for(register int t=1;t<=n;++t)
            for(register int i=1;i<=n;++i)
                  e[t][i]=min(e[t][i],e[t][k]+e[k][i]);
      }
      inline void print(){
        for(register int t=1;t<=n;++t,printf("\n"))
          for(register int i=1;i<=n;++i)
            printf("%d ",e[t][i]);
      }
}g[maxn];

inline int Min(const int&a,const int&b){if(a<b) return a;   return b;}
inline int Min(const int&a,const int&b,const int&c){return Min(a,min(b,c));}
inline int Min(const int&a,const int&b,const int&c,const int&d){return Min(a,Min(b,c,d));}

int main(){
      while(scanf("%d %d %d",&n,&m,&r)!=EOF){
        for(register int t=0;t<=n;++t)
          for(register int i=0;i<=n;++i)
            for(register int k=0;k<=n+2;++k)
                  ans[t][i][k]=inf;
        for(register int t=1;t<=n;++t) ans[t][t][0]=0;
        for(register int t=1;t<=m;++t){
          g[t].read(),g[t].gen();
          for(register int i=1;i<=n;++i)
            for(register int k=1;k<=n;++k)
                  ans[i][k][1]=min(ans[i][k][1],g[t][i][k]);
        }
        for(register int f=1;f<=n+2;++f)
          for(register int k=1;k<=n;++k)
            for(register int t=1;t<=n;++t)
                  for(register int i=1;i<=n;++i)
                    ans[t][i][f]=Min(ans[t][i][f],ans[t][i][f-1],ans[t][k][f-1]+ans[k][i][1]);
        for(register int t=1;t<=r;++t){
          register int t1=qr(),t2=qr(),t3=min(qr()+1,n+1);
          printf("%d\n",ans[t1][t2][t3]);
        }
      }
      return 0;
}

Guess you like

Origin www.cnblogs.com/winlere/p/11409244.html
car