Cows relays relay race (Fast Power shortest path matrix)

Casual working:

  FJ of N (2 <= N <= 1,000,000) cows selected as the relay to run their daily exercise program. As the relay locations to run the existing natural T (2 <= T <= 100) on the runway in a pasture. On some farms the runway intersection, each runway are connected to two different intersection I1_i and I2_i (1 <= I1_i <= 1,000; 1 <= I2_i <= 1,000). Each intersection is the endpoint of at least two runways. Cows know each runway length length_i (1 <= length_i <= 1,000), and connecting the intersection of each runway number and no two junctions are connected directly by the two different runways. You can think of these constitute a meeting point and a view of the runway. In order to complete a relay race, all N cows before running start all stand on one intersection (probably standing on some meeting point not only a cow). Of course, their stations to ensure that they can in turn pass the baton, and finally holding stick cow to stop at a preset destination. Your task is to write a program that calculates in the case of relay race starting point (S) and end (E) determined cows running the smallest possible total path length . Obviously, this path must be just after N runways .

answer:

  N shocked to see such a large, last seen n fact, the path length , which is basically a matrix of power quickly. However, the node size is 1 to 1000, obviously bear, but its edge 100, however, so it is useful to point up to 101 (communication) may be implemented in discrete (or first building side, a subscript number again DFS i.e. can). The path length as an index, the length of each side of the matrix will be directly power as quickly value matrix.

  Why should the length of each side directly as the value of the matrix? Do not shrink at? If we shrink points, 100,000 points certainly unacceptable. Furthermore, in fact, if it will seek the shortest path through the path length n, we define the matrix becomes the minimum from the node x to y z must paths, and the value is the path length of each node , so no shrink point.

  But if it is an ordinary fast power matrix, which determines the path to a point just after a few species, can not meet our expectations, we can change the matrix multiplication meaning of the questions about, becomes:

sum.a[i][j]=min(sum.a[i][j],aa.a[i][k]+bb.a[k][j]);

  It can be found to take into matrix multiplication Σ min as distributes over, so we can be in the form of a matrix multiplication become what we need.

Code:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define $ 111
 5 using namespace std;
 6 int m,n,t,start,done,in[$],sta[$*2],out[$],w[$],up;
 7 inline int min(int x,int y){    return x<y?x:y;    }
 8 struct tree{
 9     int a[$][$];
10     tree(){    memset(a,63,sizeof(a));    }
11     friend tree operator * (tree aa,tree bb){
12         tree sum=tree();
13         for(register int k=1;k<=m;++k)
14             for(register int i=1;i<=m;++i)
15                 for(register int j=1;j<=m;++j)
16                     sum.a[i][j]=min(sum.a[i][j],aa.a[i][k]+bb.a[k][j]);
17         return sum;
18     }
19     friend tree operator ^ (tree aa,int x){
20         tree sum=tree();
21         for(register int i=1;i<=m;++i) sum.a[i][i]=0;
22         for(;x;x>>=1,aa=aa*aa) if(x&1) sum=sum*aa;
23         return sum;
24     }
25 };
26 signed main(){
27     tree ans=tree();
28     scanf("%d%d%d%d",&n,&t,&start,&done);
29     for(register int i=1;i<=t;++i)
30         scanf("%d%d%d",&w[i],&in[i],&out[i]),sta[++up]=in[i],sta[++up]=out[i];
31     sta[++up]=start, sta[++up]=done;
32     sort(sta+1,sta+up+1);
33     m=unique(sta+1,sta+up+1)-sta-1;
34     for(register int i=1;i<=t;++i){
35         in[i]=lower_bound(sta+1,sta+m+1,in[i])-sta;
36         out[i]=lower_bound(sta+1,sta+m+1,out[i])-sta;
37         ans.a[in[i]][out[i]]=ans.a[out[i]][in[i]]=w[i];
38     }
39     start=lower_bound(sta+1,sta+m+1,start)-sta;
40     done= lower_bound(sta+1,sta+m+1,done)-sta;
41     ans=ans^n;
42     printf("%d\n",ans.a[start][done]);
43 }
View Code

 

Guess you like

Origin www.cnblogs.com/OI-zzyy/p/11202571.html