Cow Relays
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5411 |
|
Accepted: 2153 |
Description
For their physical fitness program, n (2≤ n ≤1,000,000) cows has decided to run a relay race using th E t (2≤ t ≤100) cow trails throughout the pasture.
Each trail Connects II different intersections (1≤ i1i ≤ 1,000; 1≤ i2i ≤1,000), EA Ch of which is the termination for at least and trails. The cows know the Lengthi of each trail (1≤ lengthi ≤1,000), the intersections of the trail connects , and they know that 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 tha n 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 T Raverses 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 i2i
Output
* Line 1: A single integer, that's the shortest distance from intersection S to intersection E . Traverses exactly N cow trails.
Sample Input
2 6 6 411 4 64 4 88 4 96 6 82 6 93 8 9
Sample Output
10
N Times Floyd, the original flody can also be like a matrix of high-speed power. A detailed view of the paper "Matrix multiplication in the application of information science"
Code:
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring>using namespace Std;const int inf= (1<<30) -1;int n;int hash[3010];//map struct matrix{int ma[210][210]; Matrix () {for (Int. i=0;i<210;i++) for (int j=0;j<210;j++) Ma[i][j]=inf; }};matrix Floyd (Matrix X,matrix y) {matrix ans; for (int i=1;i<=n;i++) {for (int. j=1;j<=n;j++) {for (int k=1;k<=n;k++) ans. Ma[i][j]=min (Ans.ma[i][j],x.ma[i][k]+y.ma[k][j]); }} return ans; Matrix pow (Matrix a,int m) {matrix ans; for (int i=1;i<=n;i++) ans.ma[i][i]=0; while (m) {if (m&1) Ans=floyd (ans,a); A=floyd (A,a); m=m>>1; } return ans; int main () {int k,t,s,e; while (~SCANF ("%d%d%d%d", &k,&t,&s,&e)) {int x,y,d; memset (hash,0,sizeof (hash)); N=1; Matrix A; for (int i=0;i<t;i++) { scanf ("%d%d%d", &d,&x,&y); if (!hash[x]) hash[x]=n++; if (!hash[y]) hash[y]=n++; A.ma[hash[x]][hash[y]]=a.ma[hash[y]][hash[x]]=d; } n=n-1; Matrix ans; Ans=pow (A,K); printf ("%d\n", Ans.ma[hash[s]][hash[e]]); } return 0;}
POJ 3613 Cow Relays