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. EZOJ #226

    传送门 分析 我们可以建一个k层图,把dp转移的三维对应到每个点上,每个第k层点连向0层点 我们让第0层点为实点其余为虚点,只要碰到虚点就dfs到他连得所有实点再将实点入队即可 代码 #include ...

  2. asp.net webform过滤器(注意我们可以在拦截请求的同时设置回调函数)

    .过滤器代码 public class PageFilter : IHttpModule { public String ModuleName { get { return "PageFil ...

  3. 02 Transcribing DNA into RNA

    Problem An RNA string is a string formed from the alphabet containing 'A', 'C', 'G', and 'U'. Given ...

  4. myeclipse的user library使用方法

    让myeclipse形成一个整齐划一的jar集合 这里就使用到了编辑器的user Library功能 首先,打开编辑器然后如图操作window--->preference--> 点开后如图 ...

  5. static 和final

    1.static       static关键字可以用来修饰类的变量,方法和内部类.static是静态的意思,也是全局的意思,它定义的东西属于全局,与类相关,不与具体实例相关.就是说它调用的时候,只是 ...

  6. C++11中的tuple应用:让函数返回多个值

    在没有tuple之前,如果函数需要返回多个值,则必须定义一个结构体,有了C++11,可以基于tuple直接做了,下面是个示例: // 编译:g++ -std=c++11 -g -o x x.cpp # ...

  7. [Lua快速了解一下]Lua的MetaTable和MetaMethod

    MetaTable和MetaMethod是Lua中的重要的语法,MetaTable主要是用来做一些类似于C++重载操作符式的功能. 两个分数 fraction_a = {numerator=, den ...

  8. ctx简介

    啥也不说,直接上图:

  9. 20145233《网络对抗》Exp8 Web基础

    20145233<网络对抗>Exp8 Web基础 实验问题思考 什么是表单? 表单在网页中主要负责数据采集功能 一个表单有三个基本组成部分: 表单标签 表单域:包含了文本框.密码框.隐藏域 ...

  10. 优化案例--改写IN条件为INNER JOIN

    --====================================== --原始语句 SET STATISTICS IO ON SELECT COUNT(DISTINCT parent_co ...