矩阵快速幂加速板题
#include<bits/stdc++.h>
#define int long long
using namespace std;
int arr[101000], tot = 0;
struct node{
int matrix[110][110];
node operator *(node b) const {
node ans; memset(ans.matrix, 0x3f, sizeof ans.matrix);
for(int k=1; k<=tot; ++k) {
for(int i=1; i<=tot; ++i) {
for(int j=1; j<=tot; ++j) {
ans.matrix[i][j] = min(matrix[i][k] + b.matrix[k][j],ans.matrix[i][j]);
}
}
}
return ans;
}
};
struct node2{
int from, to, w;
}edge[101000];
int getid(int index) {
return lower_bound(arr+1, arr+1+tot, index)-arr;
}
node Pow(node m, int u) {
if(u==1) {
return m;
}
node temp = Pow(m, u/2);
if(u&1) {
return temp*temp*m;
} else {
return temp*temp;
}
}
signed main() {
int n, t, s, e; cin >> n >> t >> s >> e;//s->e n edges
int maxn = 0;
for(int i=1; i<=t; ++i) {
cin >> edge[i].from >> edge[i].to >> edge[i].w;
arr[++tot] = edge[i].from;
arr[++tot] = edge[i].to;
}
sort(arr+1,arr+1+tot);
tot = unique(arr+1, arr+1+tot)-arr-1;
node now;memset(now.matrix, 0x3f, sizeof now.matrix);
for(int i=1; i<=t; ++i) {
now.matrix[getid(edge[i].from)][getid(edge[i].to)] = edge[i].w;
now.matrix[getid(edge[i].to)][getid(edge[i].from)] = edge[i].w;
}
if(n==1) {
cout << now.matrix[getid(s)][getid(e)];
return 0;
}
node as = Pow(now, n-1);
cout << as.matrix[getid(s)][getid(e)];
return 0;
}