1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include <iostream> #include <algorithm> #include <stdio.h> #include <cstring> #include <queue> #include <string> using namespace std; const int maxn = 200001; const int inf = 0x3f3f3f3f; struct node{ int v,t,m,next; }edge[300001]; int tot, N, M; int vis[maxn],head[maxn],cost[maxn]; long long dist[maxn]; int add(int u,int v,int time,int money) { edge[tot].v = v; edge[tot].t = time; edge[tot].m = money; edge[tot].next = head[u]; head[u] = tot++; } int spfa(int s) { dist[s] = 0; vis[s] = 1; queue<int>Q; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int k = head[u] ; k != -1; k = edge[k].next) { int v = edge[k].v; int t = edge[k].t; int m = edge[k].m; if(dist[v] > dist[u] + t || (dist[v] == dist[u] + t && cost[v] > m)) { dist[v] = dist[u] + t; cost[v] = m; if(!vis[v]) { vis[v] = 1; Q.push(v); } } } } } int main() { int T, u ,v, t ,m; scanf("%d",&T); while(T--) { scanf("%d %d",&N,&M); memset(vis,0,sizeof(vis)); memset(dist,inf,sizeof(dist)); memset(head,-1,sizeof(head)); memset(cost,inf,sizeof(cost)); tot = 1; for(int i = 1; i <= M; ++i) { scanf("%d %d %d %d", &u,&v,&t,&m); add(u,v,t,m); add(v,u,t,m); } spfa(0); long long sum = 0; long long sum1 = 0; for(int i = 1; i < N; ++ i) { sum1 += dist[i]; sum += cost[i]; } cout << sum1 << ' ' << sum << '\n'; } }
|