Cow Relays
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5651 |
|
Accepted: 2230 |
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
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cstdlib> #include <string> #include <cmath> #include <queue> #include <vector> #include < Stack>using namespace Std;int n,t,s,e,res;int hash[1010],dist[205][205],temp[205][505],f[205][205];void Floyd () { while (n) {if (n&1) {memset (temp,0x3f,sizeof (temp)); for (int k=1;k<=res;k++) for (int. i=1;i<=res;i++) for (int j=1;j<=res;j++) temp[i][j]=min (Temp[i][j],f[i] [K]+dist[k][j]); for (int. i=1;i<=res;i++) for (int j=1;j<=res;j++) F[I][J]=TEMP[I][J]; } memset (Temp,0x3f,sizeof (temp)); for (int k=1;k<=res;k++) for (int. i=1;i<=res;i++) for (int j=1;j<=res;j++) temp[i][j]=min (temp[i][j],dist[i][ K]+DIST[K][J]); for (int. i=1;i<=res;i++) for (int j=1;j<=res;j++) DIST[I][J]=TEMP[I][J]; n>>=1; } return; int main () {int z,x,y; WhiLe (scanf ("%d%d%d%d", &n,&t,&s,&e)!=eof) {res=0; memset (dist,0x3f,sizeof (Dist)); memset (F,0x3f,sizeof (f)); for (int i=0;i<=200;i++) f[i][i]=0; memset (hash,0,sizeof (hash)); for (int i=0;i<t;i++) {scanf ("%d%d%d", &z,&x,&y); if (!hash[x]) hash[x]=++res; if (!hash[y]) hash[y]=++res; Dist[hash[x]][hash[y]]=dist[hash[y]][hash[x]]=z; } Floyd (); printf ("%d\n", F[hash[s]][hash[e]]); } return 0;}
(Floyd multiplier) Poj 3613