Description
FJ's N (2 <= n <= 1,000,000) cows chose to run through the relays as their daily exercise program. As for the relay, the current T (2 <= T <= 100) runway in the farm. The runway on the farm has some intersections, and each runway is linked to two different intersections, iyuni and i2_ I (1 <= iyuni <= 1,000; 1 <= i2_ I <= 1,000 ). Each intersection is the endpoint of at least two runways. The cows know the length of each runway length_ I (1 <= length_ I <= 1,000) and the number of the intersection point connecting each runway, no two intersections are directly connected by two different runways. You can think that these intersections and runways form a picture. To complete a relay, all native cows must stand at a certain meeting point before the start of running (some meeting points may stand at more than one cow ). Of course, their positions should ensure that they can pass the baton sequentially, And the cows holding the baton will stop at the preset destination. Your task is to write a program to calculate the minimum possible total length of the running path of the cows when the start point (s) and end point (e) of the relay run are determined. Obviously, this path must exactly pass through N runways. Input
* Row 1st: Four integers separated by spaces: N, T, S, and E
* Line 2nd. t + 1: I + 1 is a space-separated INTEGER (length_ I, i1_ I, and i2_ I) that describes runway I. Output
* Row 1st: Output a positive integer, indicating the minimum length of the path that exactly passes through N runways when the start point is s and the end point is E.
Question:
If the adjacent matrix of a graph is a, a indicates the number of paths passing through an edge between each two points. A * A indicates the number of paths passing through two edges.
'A ^ n' is the number of path entries passing through n edges.
Fast Power acceleration.
Code:
# Include <cstdio> # include <cstring> # include <algorithm> # include <set> // By Zrt // problem: // no matter when you start, it is important not to stop after the start. Using namespace STD; typedef long ll; const double EPS (1e-10); const int Inf (0x3f3f3f3f); int N, T, S, E; struct n {int x, y, z;} A [105]; int to [1005]; set <int> S; int siz; int Mul [205] [205]; int map [205] [205]; void P (int A [] [205], int B [] [205]) {static int C [205] [205]; memset (C, 0x3f, sizeof C); For (INT I = 1; I <= siz; I ++) {for (Int J = 1; j <= siz; j ++) {for (int K = 1; k <= siz; k ++) {c [I] [J] = min (A [I] [k] + B [k] [J], C [I] [J]);}} Memcpy (A, C, sizeof map);} int main () {# ifdef local freopen ("in.txt", "r", stdin); freopen ("out.txt ", "W", stdout); # endif scanf ("% d", & N, & T, & S, & E); S. insert (s); S. insert (E); For (INT I = 0; I <t; I ++) {scanf ("% d", & A [I]. z, & A [I]. x, & A [I]. y); S. insert (a [I]. x); S. insert (a [I]. y) ;}for (set <int >:: iterator it = S. begin (); it! = S. end (); ++ it) {to [* It] = ++ siz;} memset (MAP, 0x3f, sizeof map); For (INT I = 0; I <t; I ++) {map [To [A [I]. y] [To [A [I]. x] = map [To [A [I]. x] [To [A [I]. y] = min (Map [To [A [I]. x] [To [A [I]. y], a [I]. z);} memcpy (Mul, MAP, sizeof map); int K = n-1; while (k) {If (K & 1) P (MAP, mul ); P (Mul, mul); k >>= 1;} printf ("% d \ n", map [To [s] [To [e]); return 0 ;}
Bzoj 1706: [usaco2007 Nov] relays dairy relay run