ZOJ 3946 Highway Project 单源最短路

来源:互联网 发布:卡密自动发货源码 编辑:程序博客网 时间:2024/06/05 21:08


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 toN - 1 (the capital is 0) and there are M highways can be built. Building thei-th highway costs Ci dollars. It takes Di minutes to travel between cityXi 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 cityi (1 ≤ iN). 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

24 50 3 1 10 1 1 10 2 10 102 1 1 12 3 1 24 50 3 1 10 1 1 10 2 10 102 1 2 12 3 1 2

Sample Output

4 34 4

Author: Lu, Yi
Source: The 13th Zhejiang Provincial Collegiate Programming Contest



#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<sstream>#include<algorithm>#include<utility>#include<vector>#include<set>#include<map>#include<queue>#include<cmath>#include<iterator>#include<stack>using namespace std;typedef long long ll;const int INF=1e9+7;const ll inf=1e15;const double eps=1e-7;void g(){ puts("Hello"); }int n,m;const int maxn=100000;struct Edge{    int from,to;    ll w,cost;    Edge(){}    Edge(int from,int to,ll w,ll cost):from(from),to(to),w(w),cost(cost){}};vector<int >G[maxn+10];vector<Edge>edges;void add_edge(int x,int y,ll w,ll cost){   edges.push_back(Edge(x,y,w,cost));   edges.push_back(Edge(y,x,w,cost));   int m=edges.size();   G[x].push_back(m-2);   G[y].push_back(m-1);}void init()//初始化{    edges.clear();    for(int i=0;i<=n;i++)    {        G[i].clear();    }}bool done[maxn+10];struct Node//dijkstra优先队列的堆元素{    int ind;    ll w,cost;    Node(){}    Node(int ind,ll w,ll cost):ind(ind),w(w),cost(cost){}    bool operator<(const Node y)const    {          return w>y.w;    }};ll d[maxn+10],Cos[maxn+10];void dijkstra()//dijkstra{    memset(done,0,(n+1)*sizeof done[0]);//    memset(d,0x3f,(n+1)*sizeof d[0]);     for(int i=0;i<=n;i++)   {       d[i]=inf;       Cos[i]=inf;//Cos[x]记录的是到x花费最小的一条边的花费,不能表示从源点到x修路的最小花费。   }   d[0]=0;   Cos[0]=0;    priority_queue<Node>q;    q.push(Node(0,0,0));    while(!q.empty())    {        Node now=q.top();q.pop();        int x=now.ind;//        cout<<x<<endl;        if(done[x])  continue;        done[x]=1;        for(int i=0;i<G[x].size();i++)        {            int id=G[x][i];            Edge &e=edges[id];            int y=e.to;//            if(done[y]) continue;            ll w=e.w;            ll cost=e.cost;            if(d[x]+w<d[y])            {                d[y]=d[x]+w;                Cos[y]=cost;                q.push(Node(y,d[y],Cos[y]) );            }            else if(d[x]+w==d[y]&&cost<Cos[y])            {                Cos[y]=cost;               q.push(Node(y,d[y],Cos[y]));            }        }    }    ll aw=0,ac=0;    for(int i=1;i<n;i++)    {        aw+=d[i];        ac+=Cos[i];    }    printf("%lld %lld\n",aw,ac);}int main(){    int T;    scanf("%d",&T);    int x,y; ll w,cost;    while(T--)    {       scanf("%d%d",&n,&m);       init();       for(int i=1;i<=m;i++)       {           scanf("%d%d%lld%lld",&x,&y,&w,&cost);           add_edge(x,y,w,cost);       }       dijkstra();    }    return 0;}



0 0
原创粉丝点击