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. Spring Boot☞ 配置文件详解:自定义属性、随机数、多环境配置等

    自定义属性与加载 我们在使用Spring Boot的时候,通常也需要定义一些自己使用的属性,我们可以如下方式直接定义: application-dev.yml com.didispace.blog: ...

  2. 【转】ConcurrentMap 分析和思考

    预备知识:Java HashMap and HashSet 的实现机制 由预备知识可以知道hashmap 的存储结构为: (图像来自http://www.ibm.com/developerworks/ ...

  3. mysql 字符串操作

    -- 字符串的长度 SELECT LENGTH('abc'),LENGTH('我的家'); SELECT CHAR_LENGTH('abc'),CHAR_LENGTH('我的家'); -- 合并字符串 ...

  4. 3.3.7 跳表 SkipList

    一.前言 concurrentHashMap与ConcurrentSkipListMap性能测试 在4线程1.6万数据的条件下,ConcurrentHashMap 存取速度是ConcurrentSki ...

  5. spring-boot-maven-plugin插件作用

    转自:http://blog.csdn.net/hotdust/article/details/51404828 OM 文件中添加了“org.springframework.boot:spring-b ...

  6. 51建设Android版一些技术整理

    不知不觉几个月就过去了,新项目已经发了两个大的版(其实已经迭代了3版),趁着项目新版刚刚上线闲下来的时间整理下用到的技术点. 整体架构 采用MVP Android官方MVP架构示例项目解析 推荐一个插 ...

  7. Type Hierarchy

    Window - Preferences - General - Keys Name:         Open Type Hierarchy Description: Open a type hie ...

  8. C#中的异步调用及异步设计模式(二)——基于 IAsyncResult 的异步设计模式

    三.基于 IAsyncResult 的异步设计模式(设计层面) IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来 ...

  9. Mac OS X Yosemite & Arduino安装CH340 USB转串口驱动

    新买的Arduino开发板 USB转串口使用了CH340芯片,在Mac OS X Yosemite上正常安装驱动后,在Arduino IDE的端口没发现相应的设备,使用以下方法后就能使用USB转串口调 ...

  10. hyper-v开发包之ddtkh

    ddtkh是微软开源社区的项目,实质是.net对象封装的一系列powershell命令,在结构上更直观更易用.它集成scvmm虚拟机管理.scom虚拟机资源监控和DPM数据保护等几种功能包.