Cows bzoj1706 relays Relay Race linear algebra

Title Description

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 go through exactly N runways.

Input Format

Line 1: 4 with a space-separated integers: N, T, S, and E
+ 1 line 2..T: i + 1 of three integers separated by a space: length_i, I1_i, and I2_i He describes the i-th runway.

Output Format

Line 1: Output a positive integer indicating the starting point is S, the end of E, N and right across the path of the minimum length of the runway

Sample

Sample input

2 6 6 4
11 4 6
4 4 8
8 4 9
6 6 8
2 6 9
3 8 9

Sample Output

10

solution:

Linear Algebra first question

If someone told me to one conclusion: For an adjacency matrix A (only 0,1 elements, there is no representation that path, not with weights),

A n elements A [i] [j] n is to take the steps from i to j number of path

Well, then what power matrix fast

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define MAXN 1000005
 5 #define MAXM 205
 6 using namespace std;
 7 int n,t,s,e,id[MAXN],tot_num=0;
 8 struct matrix{
 9     int dis[MAXM][MAXM];
10     void clear(){memset(dis,0x3f,sizeof(dis));}
11     void clean(){
12         memset(dis,0x3f,sizeof(dis));
13         for(int i=1;i<=tot_num;i++)
14             dis[i][i]=0;
15     }
16     friend matrix operator * (matrix a,matrix b){
17         matrix c;
18         c.clear();
19         for(int i=1;i<=tot_num;i++)
20             for(int j=1;j<=tot_num;j++)
21                 for(int k=1;k<=tot_num;k++)
22                     c.dis[i][j]=min(c.dis[i][j],a.dis[i][k]+b.dis[k][j]);
23         return c;
24     }
25     friend matrix operator ^ (matrix a,int b){
26         matrix c;
27         c.clean();
28         while(b){
29             if(b&1) c=c*a;
30             a=a*a;
31             b>>=1;
32         }
33         return c;
34     }
35 }ans,a;
36 int main(){
37     scanf("%d%d%d%d",&n,&t,&s,&e);
38     a.clear();ans.clean();
39     for(int i=1,l,u,v;i<=t;i++){
40         scanf("%d%d%d",&l,&u,&v);
41         if(!id[u]) id[u]=++tot_num;
42         if(!id[v]) id[v]=++tot_num;
43         u=id[u],v=id[v];
44         a.dis[u][v]=a.dis[v][u]=min(a.dis[u][v],l);
45     }
46     ans=a^n;
47     printf("%d\n",ans.dis[id[s]][id[e]]);
48     return 0;
49 }
It seems to have discrete

 

Guess you like

Origin www.cnblogs.com/Juve/p/11228203.html