数据太大,heap优化是必须的,然后不能用vector建图,即必须静态建图,因为数据太大动态建图会超时(有的提示RE),最后就是正反建图Dijkstra了,顺便一提SPFA又快又好写。。。。。。

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 10;
int T, n, m, tot, head[2][maxn];
ll dis[maxn];
struct Node {
int u, dis;
Node(int x, int y) :u(x), dis(y) {}
bool operator < (const Node &node) const {
return node.dis < dis;
}
};

struct Edge {
int to, cost, next;
}edge[2][maxn];

void init() {
tot = 0;
for (int i = 1; i <= n; i++) head[0][i] = head[1][i] = -1;
}

void addedge(int flag, int u, int v, int cost) {
edge[flag][tot].to = v;
edge[flag][tot].cost = cost;
edge[flag][tot].next = head[flag][u];
head[flag][u] = tot;
}

ll dijkstra(int flag) {
for (int i = 1; i <= n; i++) dis[i] = INF;
dis[1] = 0;
priority_queue<Node> q; q.push(Node(1, 0));
while (!q.empty()) {
Node node = q.top(); q.pop();
int u = node.u;
if (dis[u] < node.dis) continue;
for (int i = head[flag][u]; i != -1; i = edge[flag][i].next) {
int v = edge[flag][i].to, cost = edge[flag][i].cost;
if (dis[v] > dis[u] + cost) {
dis[v] = dis[u] + cost;
q.push(Node(v, dis[v]));
}
}
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += dis[i];
}
return ans;
}

int main() {
scanf("%d", &T);
while (T--) {
scanf("%d %d", &n, &m);
init();
for (int i = 1; i <= m; i++) {
int u, v, cost; scanf("%d %d %d", &u, &v, &cost);
++tot;
addedge(0, u, v, cost);
addedge(1, v, u, cost);
}
ll ans = dijkstra(0) + dijkstra(1);
printf("%I64d\n", ans);
}
return 0;
}