ZOJ3946--Highway Project--最短路+优先队列

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3946

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 Cidollars. 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

在最短路的时候时间相同看是否路程长短。利用优先队列。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll M=100000000000;
struct HeadNode
{
    ll d,u;
    ll dist;-0---------0-
    bool operator < (const HeadNode& rhs) const
    {
        if(d>rhs.d)
            return 1;
        else if(d==rhs.d&&dist>rhs.dist)
            return 1;
        return 0;//
    }
};
struct edge
{
    ll from;
    ll to;
    ll time;
    ll dist;
    ll next;
} edge[300005];
ll head[300005];
ll num=0;
void add_edge(ll from,ll to,ll time,ll dist)
{
    edge[num].from = from;
    edge[num].to = to;//终点
    edge[num].time=time;//权值
    edge[num].dist=dist;
    edge[num].next=head[from];//记录以from节点出发的上一条边在edge数组的位置
    head[from]=num;  //更新以from节点出发的在edge数组的位置
    num++; //数组下标+1
}
bool vis[300005];
ll n,m,x,s,t;
ll d[300005];
ll cost[300005];//--
void Dij()
{
    d[s]=0;
    cost[s]=0;//
    priority_queue<HeadNode>Q;
    while(!Q.empty())
        Q.pop();//
    Q.push((HeadNode)
    {
        0,s,cost[s]
    });
    while(!Q.empty())
    {
        HeadNode x=Q.top();
        Q.pop();
        ll u=x.u;
        if(vis[u])
            continue;
        vis[u]=1;
        for(ll i=head[u]; i!=0; i=edge[i].next)
        {
            ll to=edge[i].to;
            ll time=edge[i].time;
            ll dist=edge[i].dist;
            if(d[to]>d[u]+time)
            {
                d[to]=d[u]+time;//---
                cost[to]=dist;//ttt
                Q.push((HeadNode)
                {
                    d[to],to,cost[to]
                });
            }
            else if(d[to]==d[u]+time&&cost[to]>dist)
            {
                cost[to]=dist;//---///--
                Q.push((HeadNode)
                {
                    d[to],to,cost[to]
                });
            }
        }

    }
}
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        num=1;
        memset(vis,0,sizeof(vis));
        memset(head,0,sizeof(head));
        memset(edge,0,sizeof(edge));
        scanf("%lld %lld",&n,&m);//---
        s=1;
        for(ll i=1; i<=n; i++)
        {
            d[i]=M;
            cost[i]=M;
        }
        for(ll i=1; i<=m; i++)
        {
            ll x,to,time,dist;
            scanf("%lld %lld",&x,&to);//---
            x++;
            to++;
            scanf("%lld %lld",&time,&dist);
            add_edge(x,to,time,dist);
            add_edge(to,x,time,dist);
        }
        Dij();
        ll ans1=0;
        ll ans2=0;
        for(ll i=1; i<=n; i++)
        {
            ans1+=d[i];
            ans2+=cost[i];
        }
        printf("%lld %lld\n",ans1,ans2);
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值