zoj 3946(Dijkstra)

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

题意:
给定n个点和m条你可以选择建造的路(x,y,d,c)分别指x-->y需要花费d时间和建造这条路的花费c,让你在0到各个点的花费时间最少的情况下,求出总共的时间的建造路的花费。

代码:
 

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
#define INF 0x3f3f3f3f3f3f3f3f
const int maxn = 100000+10;
int n,m;

struct Edge
{
    int from,to,dist,cost;
    Edge(int f,int t,int d,int c):from(f),to(t),dist(d),cost(c){}
};

struct HeapNode
{
    int d,c,u;
    HeapNode(int d,int c,int u):d(d),c(c),u(u){}
    bool operator<(const HeapNode&rhs)const
    {
        return d>rhs.d || (d==rhs.d && c>rhs.c);
    }
};

struct Dijkstra
{
   int n,m;
   vector<Edge> edges;
   vector<int> G[maxn];
   bool done[maxn];
   ll d[maxn];
   ll c[maxn];

   void init(int n)
   {
       this->n=n;
       for(int i=0;i<n;i++) G[i].clear();
       edges.clear();
   }

   void AddEdge(int from,int to,int dist,int cost)
   {
       edges.push_back(Edge(from,to,dist,cost));
       m = edges.size();
       G[from].push_back(m-1);
   }

   void dijkstra(int s)
   {
       priority_queue<HeapNode> Q;
       for(int i=0;i<n;i++) d[i]=INF,c[i]=INF;
       d[s]=c[s]=0;
       Q.push(HeapNode(d[s],c[s],s));
       memset(done,0,sizeof(done));

       while(!Q.empty())
       {
           HeapNode x= Q.top(); Q.pop();
           int u =x.u;
           if(done[u]) continue;
           done[u]= true;

           for(int i=0;i<G[u].size();i++)
           {
               Edge &e=edges[G[u][i]];
               if(d[e.to] > d[u]+e.dist || (d[e.to]== d[u]+e.dist&&c[e.to]>e.cost)  )
               {
                   d[e.to]= d[u]+e.dist;
                   c[e.to]=e.cost;
                   Q.push(HeapNode(d[e.to],c[e.to],e.to));
               }
           }
       }
   }
}DJ;
int main()
{
    int t;
scanf("%d",&t);
    while(t--)
    {
        //int n,m;
        scanf("%d%d",&n,&m);
        DJ.init(n);
        for(int i=0;i<m;i++)
        {
            int u,v,d,c;
            scanf("%d%d%d%d",&u,&v,&d,&c);
            DJ.AddEdge(u,v,d,c);
            DJ.AddEdge(v,u,d,c);
        }

        DJ.dijkstra(0);
        ll sum1=0;
        ll sum2=0;
        /*for(int i=1;i<n;i++)
        {
            cout<<DJ.c[i]<<endl;
        }*/
        for(int i=1;i<n;i++)
        {
            sum1+=DJ.d[i];
            sum2+=DJ.c[i];
        }
       printf("%lld %lld\n",sum1,sum2);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值