吐槽: CCF绝对是最垃圾的OJ,没有之一,我内存超了,从错误类型中没看出来…..
- 为了防止内存超了,利用临接链表
- 为了防止时间超了,利用优先队列,其他的比较简答了。
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <assert.h>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXSIZE = 10005;
struct Edge{
int b,c;
Edge(int b,int c):b(b),c(c){}
bool operator<(Edge const & edge) const {
return c>edge.c;
}
};
vector<Edge> road[MAXSIZE];
int n, m;
int sum = 0;
void dij() {
vector<bool> visit(n, 0);
vector<int> cost(n, INF);
vector<int> dis(n, INF);
priority_queue<Edge> pq;
dis[0] = 0, cost[0] = 0;
pq.push(Edge(0,0));
while (!pq.empty()){
Edge edge = pq.top();
pq.pop();
visit[edge.b] = 1;
int u = edge.b;
for (int k = 0; k < road[u].size(); ++k) {
edge = road[u][k];
if (visit[edge.b]) continue;
if (dis[edge.b]>dis[u]+edge.c){
dis[edge.b] = dis[u]+edge.c;
cost[edge.b] = edge.c;
pq.push(Edge(edge.b,dis[edge.b]));
} else if (dis[edge.b] == dis[u]+edge.c){
cost[edge.b] = min(edge.c, cost[edge.b]);
}
}
}
for (int i = 0; i < n; ++i) {
sum += cost[i];
}
}
int main() {
// freopen("../in.txt", "r", stdin);
cin >> n >> m;
int a, b, c;
for (int i = 0; i < m; ++i) {
cin >> a >> b >> c;
a--, b--;
road[a].push_back(Edge(b,c));
road[b].push_back(Edge(a,c));
}
dij();
cout << sum << endl;
}