zoj 3946 Highway Project(最短路 + 优先队列)

时间:2023-03-09 16:11:43
zoj 3946 Highway Project(最短路 + 优先队列)

Highway Project


Time Limit: 2 Seconds      Memory Limit: 65536 KB

Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can reach other cities from the capital as fast as possible. Thus, he proposed the highway project.

The Marjar Empire has N cities (including the capital), indexed from 0 to N - 1 (the capital is 0) and there are M highways can be built. Building the i-th highway costs Ci dollars. It takes Di minutes to travel between city Xi and Yi on the i-th highway.

Edward wants to find a construction plan with minimal total time needed to reach other cities from the capital, i.e. the sum of minimal time needed to travel from the capital to city i (1 ≤i ≤ N). Among all feasible plans, Edward wants to select the plan with minimal cost. Please help him to finish this task.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

The first contains two integers NM (1 ≤ NM ≤ 105).

Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ XiYi < N, 0 < DiCi < 105).

Output

For each test case, output two integers indicating the minimal total time and the minimal cost for the highway project when the total time is minimized.

Sample Input

2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 1 1
2 3 1 2
4 5
0 3 1 1
0 1 1 1
0 2 10 10
2 1 2 1
2 3 1 2

Sample Output

4 3
4 4

题意:从0点开始到各点距离最小,在距离最小情况下还要花费最少,做这题的时候题目没搞明白,

优先队列维护,

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
const LL INF = ((LL) ) << ;
const int Max = 1e5 + ;
struct Edge
{
int v, d, c;
int Next;
};
struct Node
{
int u;
LL d, c;
friend bool operator<(const Node & a, const Node & b)
{
if (a.d == b.d)
return a.c > b.c;
return a.d > b.d;
}
};
Node node[Max];
Edge edge[Max * ];
int head[Max];
int vis[Max];
int n, m, tot;
void addEdge(int u, int v, int d, int c)
{
edge[tot].v = v; edge[tot].d = d; edge[tot].c = c;
edge[tot].Next = head[u];
head[u] = tot++; edge[tot].v = u; edge[tot].d = d; edge[tot].c = c;
edge[tot].Next = head[v];
head[v] = tot++;
} void dijstra()
{
for (int i = ; i <= n; i++)
{
node[i].u = i;
node[i].d = INF;
node[i].c = INF;
}
priority_queue<Node> que;
node[].c = node[].d = ;
que.push(node[]);
memset(vis, , sizeof(vis)); while (!que.empty())
{
Node temp = que.top();
que.pop();
int u = temp.u;
if (vis[u])
continue;
vis[u] = ;
for (int i = head[u]; i != -; i = edge[i].Next)
{
if (vis[ edge[i].v ])
continue;
if (node[ edge[i].v ].d > node[ u ].d + edge[i].d)
{
node[ edge[i].v ].d = node[ u ].d + edge[i].d;
node[ edge[i].v ].c = edge[i].c;
que.push(node[ edge[i].v ]);
}
else if (node[ edge[i].v ].d == node[ u ].d + edge[i].d && node[ edge[i].v ].c > edge[i].c)
{
node[ edge[i].v ].c = edge[i].c;
que.push(node[ edge[i].v ]);
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
int u, v, d, c;
tot = ;
for (int i = ; i <= m; i++)
{
scanf("%d%d%d%d", &u, &v, &d, &c);
addEdge(u, v, d, c);
}
dijstra();
LL sum_d = , sum_c = ;
for (int i = ; i < n; i++)
{
sum_d += node[i].d;
sum_c += node[i].c;
}
printf("%lld %lld\n", sum_d, sum_c);
}
return ;
}