Source: Internet
Author: User

Tags: properly technology ret shortest way describes space namespace least POJ

Descriptionfor Their physical fitness program, N (2≤n≤1,000,000) cows has decided to run a relay race using the T (2 ≤T≤100) cow trails throughout the pasture. Each trail connects-different intersections (1≤i1i≤1,000; 1≤i2i≤1,000), each of the which are the termination for a t least and trails. The cows know the lengthi of each trail (1≤lengthi≤1,000), the intersections of the trail connects, and they know th At no, intersections is directly connected by the 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 has more than one cow ). They must position themselves properly so this they can hand off the baton Cow-by-cow and end up at the proper finishing P Lace. 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:n, T, S, and e* Lines 2..t+1:line i+1 describes trail I with three space-separated Integers:lengthi, i1i, and i2ioutput* Line 1: A single Integer That's the shortest distance from intersection S to int Ersection E that traverses exactly N cow trails. Sample Input2 6 6 411 4 4 4 6 6 8 9Sample Output10sourceusaco November Goldsurface

A matrix A (I, j) is used to denote the shortest path from I to J through several edges,

Initialize a to the length of the I to J side, not the positive infinity.

For example, a matrix is represented by an n-bar, B-matrix is represented by M-Bar,

Then A * b gets a matrix representing the M + N edges,

The idea of using Floyd is updated.

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <cmath>6#include <queue>7#include <string>8#include <map>9 #definell Long LongTen using namespacestd; One ll N,m,s,t,l; AMap<ll,ll>ID; - structnode{ -ll a[202][202]; theFriend nodeoperator*(node X,node y) - { - node Z; -memset (Z.A,0x3f,sizeof(Z.A)); + for(LL k=1; k<=l;k++) - for(LL i=1; i<=l;++i) + for(LL j=1; j<=l;++j) AZ.a[i][j]=min (z.a[i][j],x.a[i][k]+y.a[k][j]); at returnZ; - } - }s,ans; - voidKSM () - { -ans=s; inn--; - while(n) to { + if(n&1) ans=ans*s; -s=s*s; then>>=1; * } $ }Panax Notoginseng intMain () - { theFreopen ("run.in","R", stdin); +Freopen ("Run.out","W", stdout); AMemset (S.A.,0x3f,sizeof(S.A.)); thescanf"%lld%lld%lld%lld",&n,&m,&s,&T); + for(LL i=1, x,y,z;i<=m;++i) - { $scanf"%lld%lld%lld",&z,&x,&y); $ if(Id[x]) x=Id[x]; - Elsel++,id[x]=l,x=l; - if(Id[y]) y=Id[y]; the Elsel++,id[y]=l,y=l; -s.a[x][y]=s.a[y][x]=Z;Wuyi } theS=id[s]; t=Id[t]; - KSM (); Wuprintf"%lld", Ans.a[s][t]); - return 0; About } $ /* - 2 6 6 4 - 4 6 - 4 4 8 A 8 4 9 + 6 6 8 the 2 6 9 - 3 8 9 $ Ten the */View Code

POJ 3613Cow Relays