K - Highway Project

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 Tindicating 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;
typedef pair<pair<int,int>,int>P;//放入队中的pair嵌套,第一个first对应pair(Time,Cost),second对应顶点编号)
typedef pair<int,int>Pi;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int t,n,m,cnt;
int head[maxn],vis[maxn],cost[maxn];
ll dis[maxn];//dis在更新过程中可能会超过int
struct node
{
    int v,Time,Cost,next;//所指向顶点,花费时间,花费金钱…………
}e[maxn*2];//无向图最大边数乘以2
void add(int u,int v,int Time,int Cost)
{
    e[cnt].v=v;
    e[cnt].Time=Time;
    e[cnt].Cost=Cost;
    e[cnt].next=head[u];
    head[u]=cnt++;
}
void Init()
{
    cnt=1;
    memset(vis,0,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    memset(cost,INF,sizeof(cost));
    memset(head,-1,sizeof(head));
}
void Dijkstra(int s)
{
    priority_queue<P,vector<P>,greater<P> >que;
    que.push(P(Pi(0,0),0));
    dis[s]=cost[s]=0;
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int u=p.second;
        if(vis[u])
            continue;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].v;
            int Time=e[i].Time;
            int Cost=e[i].Cost;
            if(dis[v]>dis[u]+Time)//保证时间最少
            {
                dis[v]=dis[u]+Time;
                cost[v]=Cost;
                que.push(P(Pi(dis[v],cost[v]),v));
            }
            else if(dis[v]==dis[u]+Time&&cost[v]>Cost)//时间最少的情况下降低费用,这里只记录该点的一个cost即可
            {
                cost[v]=Cost;
            }
        }
    }
}
int main()
{
    std::ios::sync_with_stdio(0);
    cin>>t;
    while(t--)
    {
        Init();
        ll sum1=0,sum2=0;//最短时间,最少花费
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            int u,v,t,c;
            cin>>u>>v>>t>>c;
            add(u,v,t,c);
            add(v,u,t,c);
        }
        Dijkstra(0);
        for(int i=0;i<n;i++)
        {
            sum1+=dis[i];
            sum2+=cost[i];
        }
        cout<<sum1<<" "<<sum2<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值