Test instructions
There are n cities, numbered 1~ you, M-Road, each road starting point is U, the end point is V, and the toll from U to V is cost, starting from 1, to get the minimum spend at k time to reach N, if the k time is unreachable n, then output-1
Method: flody+ Matrix Fast Power
Note: After the array invokes the function, its value will change (the function call of the array actually passes the position)
(Detailed http://blog.chinaunix.net/uid-16875687-id-2155704.html)
1#include <stdio.h>2 3 intFintb[2][2])4 {5 for(intI=0;i<2; i++)6 {7 for(intj=0;j<2; j + +)8b[i][j]++;9 }Ten } One intMain () A { - inta[2][2]; - for(intI=0;i<2; i++) the { - for(intj=0;j<2; j + +) -scanf"%d",&a[i][j]); - } + f (a); - for(intI=0;i<2; i++) + { A for(intj=0;j<2; j + +) atprintf"%d", A[i][j]); - } - return 0; -}
This problem can be understood as a K-Flody cycle (a city can be repeated), K value is too large, with the matrix to reduce the time of a fast power
1#include <iostream>2#include <algorithm>3 using namespacestd;4 #defineINF 1000000000000000LL//define default is int, the value is too large when plus ll5 #defineN 556 __int64 Ma[n][n],temp[n][n];7 intN;8 voidFloy (__int64 a[n][n])9 {Ten inti,j,k; One for(i=1; i<=n;i++) A for(j=1; j<=n;j++) -TEMP[I][J] =INF; - for(k=1; k<=n;k++) the for(i=1; i<=n;i++) - for(j=1; j<=n;j++) -Temp[i][j] = min (temp[i][j],a[i][k]+ma[k][j]); - for(i=1; i<=n;i++) + for(j=1; j<=n;j++) -A[I][J] =Temp[i][j]; + } A at voidFintK,__int64 Dis[n][n]) - { -k--; - while(k) - { - if(k&1) Floy (DIS);//Judging the odd and even odd &1=1 even &1=0 in Floy (MA); -k=k/2; to } + } - the intMain () * { $ intt,m,k;Panax Notoginseng inti,j; - intu,v; the __int64 cost; + __int64 Dis[n][n]; ACin>>T; the while(t--) + { -Cin>>n>>m>>K; $ for(i=1; i<=n;i++) $ for(j=1; j<=n;j++) - { -dis[i][j]=INF; thema[i][j]=INF; - }Wuyi the for(i=1; i<=m;i++) - { WuCin>>u>>v>>Cost ; - if(Ma[u][v]>cost)//Heavy Edge About { $ma[u][v]=Cost ; -dis[u][v]=Ma[u][v]; - } - } A + f (k,dis); the - if(dis[1][n]>=inf) cout<<"-1"<<Endl; $ Elsecout<<dis[1][n]<<Endl; the } the return 0; the}
View Code
Fzu 2173 Nostop