如题,这题检查了好久,一开始是40pts,改了e数组大小后变成50pts了
是dijkstra+堆优化
如果有dalao帮忙看一下代码 或者帮忙说一下检查的方向 窝会感激不尽的/kk 谢谢!
(已上网查)
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define pairs pair<int, int>
#define maxn 800
#define maxm 1500
using namespace std;
int n, p, c;
int sum[805];
int tot, h[maxn];
int dis[maxn];
bool vis[maxn];
int minn = 0x3f3f3f3f;
struct edge
{
int v, w, next;
}e[maxm >> 1];
void add_edge(int u, int v, int w){
e[++tot].next = h[u];
h[u] = tot;
e[tot].v = v;
e[tot].w = w;
//cout<<"# "<<h[u]<<" "<<e[tot].v<<" "<<e[tot].w<<" "<<e[tot].next<<endl;
}
void dijkstra(int u1){
priority_queue<pairs, vector<pairs>, greater<pairs> > q;
memset(dis, 0x3f, sizeof(dis));
memset(vis, false, sizeof(vis));
dis[u1] = 0;
q.push(make_pair(dis[u1], u1));
while(!q.empty()){
int d = q.top().second;
q.pop();//之前忘记了
if(vis[d]) continue;
vis[d] = true;
for(int i = h[d]; i; i = e[i].next){
int v = e[i].v;
if(dis[v] > dis[d] + e[i].w){
dis[v] = dis[d] + e[i].w;
//dis[v] *= sum[v];
q.push(make_pair(dis[v], v));
}
}
}
}
int main(){
cin>>n>>p>>c;
int a, u, v, w;
for(int i = 1; i <= n; i++){
cin>>a;
sum[a] ++;
}
for(int i = 1; i <= c; i++){
cin>>u>>v>>w;
add_edge(u, v, w);
add_edge(v, u, w);
}
for(int i = 1; i <= p; i++){
dijkstra(i);
int summ = 0;
for(int j = 1; j <= p; j++){
summ += dis[j] * sum[j];
}
if(summ < minn) minn = summ;
}
cout<<minn;
return 0;
}