zoj 3946 Highway Project (spfa + 最小生成树)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718

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 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
题意:给出N个点,编号(0-N-1),M条边,每两个点之间要修一条路,时间T和花费C,求在时间最短的情况下花费最小

思路:最短路求最短时间的同时更新最小花费(最小生成树)

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;

typedef long long LL;
#define N 100020
const LL  INF = (1ll<<60)-1;
#define met(a, b) memset (a, b, sizeof(a))

int head[N], vis[N], cnt, n, m;
LL COST[N], TIME[N];

struct node
{
    int u, v, next;
    LL cost, Time;
}edge[N*4];

void Init ()
{
    met (head, -1);
    met (vis, 0);
    cnt = 0;

    for (int i=0; i<n; i++)
    {
        TIME[i] = INF;
        COST[i] = INF;
    }
}

void Add_Edge (int u, int v, LL cost, LL Time)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].cost = cost;
    edge[cnt].Time = Time;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}

void SPFA ()
{
    queue <int> que;
    TIME[0] = 0;
    COST[0] = 0;
    //vis[0] = 1;

    que.push (0);

    while (que.size())
    {
        int u = que.front(); que.pop();
        vis[u] = 0;

        for (int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v = edge[i].v;
            LL cost = edge[i].cost;
            LL Time = edge[i].Time;

            if (TIME[v] > TIME[u] + Time || (TIME[v] == TIME[u] + Time && COST[v] > cost))
            {
                TIME[v] = TIME[u] + Time;
                COST[v] = cost;

                if (!vis[v])
                {
                    vis[v] = 1;
                    que.push (v);
                }
            }
        }
    }
}

int main ()
{
    int t;
    scanf ("%d", &t);

    while (t--)
    {
        int u, v;
        LL cost, Time;
        scanf ("%d %d", &n, &m);
        Init ();

        while (m--)
        {
            scanf ("%d %d %lld %lld", &u, &v, &Time, &cost);
            Add_Edge (u, v, cost, Time);
            Add_Edge (v, u, cost, Time);
        }

        SPFA ();

        LL ans1 = 0, ans2 = 0;
        for (int i=0; i<n; i++)
        {
            ans1 += TIME[i];
            ans2 += COST[i];
        }

        printf ("%lld %lld\n", ans1, ans2);
    }
    return 0;
}

你可能感兴趣的:(zoj 3946 Highway Project (spfa + 最小生成树))