ZOJ3946 Highway Project #最短路 Dijkstra算法#

Highway Project

Time Limit: 2000 msMemory 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 N, M (1 ≤ N, M ≤ 105).

Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 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

Author: LU, Yi

Solution

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> p;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
bool vis[maxn];
int cnt, head[maxn];
ll dis[maxn], cost[maxn];
struct edge { int to, nxt, dis, cost; } e[maxn << 1];
struct node
{
    int id;
    ll dis, cost;
    bool operator < (const node& x) const
    {
        if (dis != x.dis) return dis > x.dis;
        return cost > x.cost;
    }
};
priority_queue<node> q;

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1; char ch = getchar();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar(); }
    return x * f;
}

template<typename T>
inline void write(T x)
{
    if (x < 0) { putchar('-'); x = -x; }
    if (x > 9) write(x / 10);
    putchar(x % 10 + '0');
}

void addEdge(int u, int v, int d, int c)
{
    e[cnt].to = v;
    e[cnt].dis = d;
    e[cnt].cost = c;
    e[cnt].nxt = head[u];
    head[u] = cnt++;
}

void dijkstra(int src)
{
    dis[src] = cost[src] = 0;
    q.push(node{ src, 0, 0 });
    while (!q.empty())
    {
        int u = q.top().id;
        q.pop();
        if (vis[u]) continue;
        vis[u] = true;
        for (int i = head[u]; ~i; i = e[i].nxt)
        {
            int v = e[i].to, d = e[i].dis, c = e[i].cost;
            if (dis[v] > dis[u] + d)
            {
                dis[v] = dis[u] + d;
                cost[v] = c;
                q.push(node{ v, dis[v], cost[v] });
            }
            else if (dis[v] == dis[u] + d && cost[v] > c)
            {
                cost[v] = c;
                q.push(node{ v, dis[v], cost[v] });
            }
        }
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("input.txt", "r", stdin);
#endif
    int t = read();
    while (t--)
    {
        cnt = 0;
        memset(head, -1, sizeof(head));
        memset(vis, false, sizeof(vis));
        memset(dis, 0x3f, sizeof(dis));
        memset(cost, 0x3f, sizeof(cost));
        int n = read(), m = read();
        while (m--)
        {
            int u = read(), v = read(), d = read(), c = read();
            addEdge(u, v, d, c);
            addEdge(v, u, d, c);
        }
        dijkstra(0);
        ll dsum = 0, csum = 0;
        for (int i = 0; i < n; i++)
        {
            dsum += dis[i];
            csum += cost[i];
        }
        cout << dsum << " " << csum << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_35850147/article/details/104863447
今日推荐