一开始看复杂度挺吓人的毕竟跑 n次最短路就是 n * m
一共就是 1000 * (1000 * 999)
但是写出来之后还是没有超思路就是跑n遍最短路
然后遍历m条边 看哪个最小
#include<iostream>
#include<utility>
#include<queue>
#include<cstring>
#define x first
#define y second
using namespace std;
const int N = 1e3 + 10,M = 2e3 + 100;
typedef pair<int,int> PII;
int head[N],to[M],last[M],w[M],cnt;
void add(int a,int b,int c){
w[++cnt] = c;
to[cnt] = b;
last[cnt] = head[a];
head[a] = cnt;
}
int dist[N][N];
int flag[N],now;
void dij(int x){
priority_queue<int,vector<PII>,greater<PII> > q;
q.push({0,x});
dist[x][x] = 0;
while(q.size()){
PII p = q.top();
q.pop();
if(flag[p.y] != now){
flag[p.y] = now;
}else continue;
for(int i = head[p.y]; i != -1; i = last[i]){
int j = to[i];
if(dist[x][j] > dist[x][p.y] + w[i]){
dist[x][j] = dist[x][p.y] + w[i];
q.push({dist[x][j],j});
}
}
}
}
int a[N],b[N],c[N],u[N],v[N];
int main(){
int n,m,k;
memset(head,-1,sizeof head);
memset(dist,0x3f,sizeof dist);
scanf("%d%d%d",&n,&m,&k);
for(int i = 1; i <= m; i++){
scanf("%d%d%d",&a[i],&b[i],&c[i]);
add(a[i],b[i],c[i]);
add(b[i],a[i],c[i]);
}
for(int i = 1; i <= n; i++){
++now;
dij(i);
}
for(int i = 1; i <= k; i++){
scanf("%d%d",&u[i],&v[i]);
}
/*for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cout << dist[i][j] << " ";
}
cout << endl;
}*/
int maxn = 0x3f3f3f3f,ans;
for(int i = 1; i <= m; i++){
ans = 0;
for(int j = 1; j <= k; j++){
ans += min(dist[u[j]][v[j]],min(dist[u[j]][a[i]] + dist[b[i]][v[j]],dist[u[j]][b[i]] + dist[a[i]][v[j]]));
}
maxn = min(maxn,ans);
}
printf("%d\n",maxn);
return 0;
}