POJ3613--Cow Relays

Description

For their physical fitness program, N (2 ≤ N ≤ 1,000,000) cows have decided to run a relay race using the T (2 ≤ T ≤ 100) cow trails throughout the pasture.

Each trail connects two different intersections (1 ≤ I1i ≤ 1,000; 1 ≤ I2i ≤ 1,000), each of which is the termination for at least two trails. The cows know the lengthi of each trail (1 ≤ lengthi  ≤ 1,000), the two intersections the trail connects, and they know that no two intersections are directly connected by two different trails. The trails form a structure known mathematically as a graph.

To run the relay, the N cows position themselves at various intersections (some intersections might have more than one cow). They must position themselves properly so that they can hand off the baton cow-by-cow and end up at the proper finishing place.

Write a program to help position the cows. Find the shortest path that connects the starting intersection (S) and the ending intersection (E) and traverses exactly N cow trails.

Input

* Line 1: Four space-separated integers: NTS, and E
* Lines 2..T+1: Line i+1 describes trail i with three space-separated integers: lengthi , I1i , and I2i

Output

* Line 1: A single integer that is the shortest distance from intersection S to intersection E that traverses exactly N cow trails.

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 
too lazy to translate (in fact, is not)
that Italy: From e s arrival point after point and n edges of the shortest number is (can walk duplicate path)

   Ideas: folyd can add edge to achieve the path, but this question and seek common shortest path problem is not the same, such as from S to E through X after the sides have reached the shortest, this time with floyd still want to force Adds side, although not added after the shortest side, and it is to be noted here that the greatest short-circuited to add minimal loss, grasp this operation force can be realized by adding floyd side, it is possible to state from the n = 1 n state transition, then you will find a man named N  (2 ≤  N  ≤ 1,000,000) of * & # ¥% ...... @! ** & # @ ¥! % ¥ ...... & something, then you fry, so to optimize power use fast, so you only log (n) will be able to find it.

There is also a most important method, you have to know one way to go through a little bit of a road through the + = a road through two points, and so on can be obtained through a two-way road through the + = a little after three a waypoint.

This is a method of matrix multiplication, then you will find if a matrix, k represents the down edges, one of the dots of FIG shortest path , (a, b) represents a shortest path from a to b, then we own it, a matrix multiplication format "multiplied" by which to take the min, CA [I] [J] = min (CA [I] [J], XA [I] [K ] + ya [k] [j ])

Thus obtained is to take k + k edges matrix. A little abstract, explained in detail below, as follows :

A point (a, b) in C, when we find it with a matrix x and y matrix, we enumerates a number of all the matrix rows x, y and the number of all columns of matrix b, and they can coordinate a corresponding, such as the matrix x (a, 2) at this point, the corresponding point is the y-matrix (2, b), then up into the FIG appreciated, i.e. over a distance from a point b to point 2 points, similar to the not only two points, all the points after enumeration, CA [a] [b] is the shortest distance from a to b,. (Let's imagine)

This way, the down will be k + k edges of the shortest path, so that the operation of the other matrix, the two of them is obtained, after the addition result of the number of sides. (After a side of a matrix and a bar b edges after this operation the matrix, the matrix is ​​the result of the edges of a + b, stored in the matrix is ​​the shortest path). Like the above example, (a, 2) (2, b), i.e. is the distance from point a through point b to point 2, since the matrix x and y are both shortest path matrix after k edges, then x matrix (a, 2) take the shortest path after k steps, (2, b) is, they take the sum is added to the shortest path K edges.

Then, you can apply a template to quickly power, but will take into previously added, that is, the idea of ​​doubling, for example walking 10 side, it is binary 1010, then we let the two sides go when the matrix is ​​multiplied by a matrix edge 8, to give (binary multiplying) take the side of the matrix 10 at the beginning i.e. from 1-> 2-> 4-> 8-> 16 ...... i.e. n-th power of 2.

Code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<map>
 4 #include<cstring>
 5 #include<iostream>
 6 using namespace std;
 7 int k,t,sb,e;
 8 int ji=0;
 9 int head[11000];
10 struct str{
11     int a[201][201];
12     str operator * (const str &x) const
13     {
14         str c;
15         memset(c.a,0x3f,sizeof(c.a));
16         for(int k=1;k<=ji;k++){
17             for(int i=1;i<=ji;i++){
18                 for(int j=1;j<=ji;j++){
19                     c.a[i][j]=min(c.a[i][j],a[i][k]+x.a[k][j]);
20                 }   
21             }
22         }
23         return c;
24     } 
25 }s,ans;
26 void quickly(){
27     ans=s;
28     k--;
29     while(k)
30     {
31         if(k&1) ans=ans*s;
32         k>>=1;
33         s=s*s;
34     }
35 }
36 int main(){
37     cin>>k>>t>>sb>>e;
38     memset(s.a,0x3f,sizeof(s.a));
39     for(int i=1;i<=t;i++){
40         int x,y,z;
41         cin>>z>>x>>y;
42         if(!head[x]) head[x]=++ji;
43         if(!head[y]) head[y]=++ji;
44         s.a[head[x]][head[y]]=z;
45         s.a[head[y]][head[x]]=z;
46     }
47     quickly();
48     cout<<ans.a[head[sb]][head[e]];
49 }
50 /*
51 12 6 6 4
52 11 4 6
53 4 4 8
54 8 4 9
55 6 6 8
56 2 6 9
57 3 8 9
58 */

Guess you like

Origin www.cnblogs.com/dai-jia-ye/p/11068595.html