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

题解:我们取得第一优先级是时间,其次是花费,但是更新花费时会出现重复的,我们的思路就变成了只有在中转点的路径才更新花费,只更新新增加的一部分,这是重要的点,如果距离相等,我们看直接的花费少还是有中转点花费少

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#define INF 9999999999999
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int n,m;
ll ttime[maxn],cost[maxn];
struct node
{
ll to,cost,time;
bool friend operator < ( node x,node y )
{
return x.time>y.time;
}
};
vector<node>vec[maxn];
void init()
{
for(int t=0;t<n;t++ )
{
vec[t].clear();
}
}
void Addedge()
{
int u,v,time,cost;
for(int t=0;t<m;t++)
{
scanf("%lld%lld%lld%lld",&u,&v,&time,&cost);
node Map1,Map2;
Map1.to=u;
Map1.time=time;
Map1.cost=cost;
Map2.to=v;
Map2.time=time;
Map2.cost=cost;
vec[Map1.to].push_back(Map2);
vec[Map2.to].push_back(Map1); }
}
void Dij(int start)
{
for(int t=0;t<n;t++ )
{
ttime[t]=INF;
cost[t]=INF;
}
ttime[start]=0;
cost[start]=0;
node sta;
sta.to=start;
sta.cost=0;
sta.time=0;
priority_queue<node>q;
q.push(sta);
while(!q.empty())
{
node now=q.top();
q.pop();
int to=now.to;
for(int t=0;t<vec[to].size();t++)
{
node temp;
temp=vec[to][t];
if(temp.time+ttime[now.to]<ttime[temp.to])
{
ttime[temp.to]=temp.time+ttime[now.to];
cost[temp.to]=temp.cost;
node next;
next.to=temp.to;
next.time=ttime[temp.to];
next.cost=cost[temp.to];
q.push(next);
}
else if(temp.time+ttime[now.to]==ttime[temp.to])
{
cost[temp.to]=min(cost[temp.to],temp.cost);
}
}
} }
int main()
{
int T;
cin>>T;
while(T--)
{
init();
cin>>n>>m;
Addedge();
Dij(0);
ll sum1=0,sum2=0;
for(int t=0;t<n;t++)
{
sum1+=ttime[t];
}
for(int j=0;j<n;j++)
{
sum2+=cost[j];
}
cout<<sum1<<" "<<sum2<<endl; }
return 0;
}

ZOJ - 3946-Highway Project(最短路变形+优先队列优化)的更多相关文章

  1. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  2. ZOJ 3946 Highway Project (最短路)

    题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...

  3. ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA

    ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the ...

  4. ZOJ 3946 Highway Project(Dijkstra)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  5. POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...

  6. ZOJ 3946 Highway Project 贪心+最短路

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...

  7. ZOJ 3946 Highway Project

    1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...

  8. Gym 101873C - Joyride - [最短路变形][优先队列优化Dijkstra]

    题目链接:http://codeforces.com/gym/101873/problem/C 题意: 这是七月的又一个阳光灿烂的日子,你决定和你的小女儿一起度过快乐的一天.因为她真的很喜欢隔壁镇上的 ...

  9. ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP

    K - Watermelon Full of Water Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld &am ...

随机推荐

  1. linux环境下搭建osm_web服务器二(Mapnik及apache2mod_tile配置):

    Mapnik及apache2mod_tile配置 上一篇,我们配置好了PostgreSQL服务器,导入了测试数据.今天,我们来配置 mapnik2 + apache2 + mod_tile 的WMS服 ...

  2. 点云数据保存为pcd文件_pcd_write.cpp

    #include <iostream>#include <pcl/io/pcd_io.h>#include <pcl/point_types.h> int main ...

  3. 实践作业3:白盒测试----第三次小组会DAY8

    我们小组于12月11日在东九教学楼召开了会议,小组第二次会议中就讨论了被测系统中风险最高的模块,于是选择管理员的教师添加模块,针对代码展开静态评审.这一次会议,小组主要讨论了应该从哪些方面来测评代码的 ...

  4. JS作用域理解(声明提升)

    1.JS解析步骤: a.预解析 将变量声明提升: 将函数声明及函数内容提升,可以理解成原来位置的函数在解析代码时已经提到代码初始位置: 块内的变量声明和函数声明也会被提升,例如if语句 遇到重名,只留 ...

  5. 1056 IMMEDIATE DECODABILITY

    题目链接: http://poj.org/problem?id=1056 题意: 给定编码集, 判断它是否为可解码(没有任何一个编码是其他编码的前缀). 分析: 简单题目, 遍历一遍即可, 只需判断两 ...

  6. C++11新特性之auto

    auto的使用  c++11引入了auto类型说明符,auto让编译器通过初始值来推算变量的类型,所以auto定义的变量必须有初始值.  使用auto也能在一条语句中声明多个变量,因为一条声明语句只能 ...

  7. Git: 教你如何在Commit时有话可说

    Git: 教你如何在Commit时有话可说   不知道大家有没有观察过那些在Github上Star数位居前列的项目,它们无一例外的都拥有完善的文档体系和高覆盖的测试用例.要做到完善没有规范肯定是不行的 ...

  8. page next page prev

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  9. Android 控件在布局中按比例放置[转]

    转自:http://netsky1990.blog.51cto.com/2220666/997452       在Android开发中常用到线性布局LinearLayout对界面进行具体的创建,其中 ...

  10. 编写高质量代码改善C#程序的157个建议——建议97:优先考虑将基类型或接口作为参数传递

    建议97:优先考虑将基类型或接口作为参数传递 除了公开及类型或接口外,方法的参数也应该考虑基类型或接口. 以Enumerable类型为例,它的成员方法中只要涉及需要操作集合对象的地方,都要使用IEnu ...