题目描述:
dijkstra方法运行超时,Floyed算法+优先队列30分
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=10010;
const int inf=0x3f3f3f3f;
int graph[N][N];
int n,m,k;
int xing[N];
priority_queue<int,vector<int>,greater<int> >q;
void Floyed(){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(graph[i][j]>graph[i][k]+graph[k][j]){
graph[i][j]=graph[i][k]+graph[k][j];
}
}
}
}
}
int main(){
cin.tie(0);
cout.tie(0);
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++){
scanf("%d",&xing[i]);
}
//路径初始化
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j){
graph[i][j]=0;
}else{
graph[i][j]=inf;
}
}
}
int u,v,w;
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
graph[u][v]=graph[v][u]=min(graph[u][v],w);
}//数据输入结束
Floyed();
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(graph[i][j]<inf&&xing[j]){
q.push(graph[i][j]);
}
}
ll sum=0,cnt=0;
while(!q.empty()){
sum+=q.top();
q.pop();
cnt++;
if(cnt==k) break;
}
printf("%lld\n",sum);
while(!q.empty()) q.pop();
}
return 0;
}