Subject to the effect
There is a demand in the \ (m \) edges undirected connected graphs, point \ (S \) point \ (T \) through \ (K \) shortest of edges ( \ (. 1 \ Leq m \ Leq 100 \) , \ (. 1 \ Leq K \ Leq. 6 10 ^ \) ).
answer
Number of edges \ (m \) is small, clearly points \ (n-\) is also very small, not more than \ (200 \) , we can first discretization.
We can get \ (n-\) adjacency matrix between points, wherein the matrix \ (a [i] [j ] \) is equivalent to the point \ (I \) point \ (J \) through \ (1 \) shortest edges of .
In this case we set \ (dp [i] [j ] [k '] \) represents the point \ (I \) point \ (J \) through \ (k' \) shortest sides is apparently a
\ [dp [i] [j]
[1] = a [i] [j] \] and
\ [dp [i] [j ] [k '] = \ min \ {dp [i] [l] [k' - 1] + dp [l]
[j] [k '- 1] \}, k'> 1 \] clearly, simple complexity of dp \ (O (KN ^ 2) \) .
This process is observed, in fact, much like the matrix multiplication, the same, we can also use a matrix to optimize power quickly, so that the complexity of the drops to $ O (n ^ 2 \ log {k}) $ a.
#include <iostream>
#include <cstring>
#include <algorithm>
#define MAX_M (100 + 5)
using namespace std;
int K, m, s, t;
int u[MAX_M], v[MAX_M];
long long w[MAX_M];
int tmp[MAX_M << 1];
int to[1000005], n;
struct Matrix
{
long long mat[MAX_M][MAX_M];
Matrix()
{
memset(mat, 0x3f, sizeof mat);
return;
}
friend Matrix operator * (Matrix a, Matrix b)
{
Matrix c;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
for(int k = 1; k <= n; ++k)
{
c.mat[i][j] = min(c.mat[i][j], a.mat[i][k] + b.mat[k][j]);
}
}
}
return c;
}
friend Matrix operator *= (Matrix & a, Matrix b)
{
return a = a * b;
}
};
Matrix a;
int main()
{
cin >> K >> m >> s >> t;
for(int i = 1; i <= m; ++i)
{
cin >> w[i] >> u[i] >> v[i];
tmp[i << 1 ^ 1] = u[i];
tmp[i << 1] = v[i];
}
sort(tmp + 1, tmp + m + m + 1);
for(int i = 1; i <= (m << 1); ++i)
{
if(tmp[i] != tmp[i - 1]) to[tmp[i]] = ++n;
}
for(int i = 1; i <= m; ++i)
{
u[i] = to[u[i]];
v[i] = to[v[i]];
a.mat[u[i]][v[i]] = min(a.mat[u[i]][v[i]], w[i]);
a.mat[v[i]][u[i]] = min(a.mat[v[i]][u[i]], w[i]);
}
--K;
Matrix res = a;
while(K)
{
if(K & 1) res *= a;
a *= a;
K >>= 1;
}
cout << res.mat[to[s]][to[t]];
return 0;
}