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. Java泛型:泛型的定义(类、接口、对象)、使用、继承

    地址   http://blog.csdn.net/lirx_tech/article/details/51570138 1. 设计泛型的初衷: 1) 主要是为了解决Java容器无法记忆元素类型的问题 ...

  2. oracle 环境变量(中文显示乱码)

     NLS_LANGSIMPLIFIED CHINESE_CHINA.ZHS16GBK 

  3. CSS3 圆环状进度条

    HTML: <div class="wrap"> <div class="progress-radial progress-25"> & ...

  4. Windows 配置 nginx php 多版本切换

    1. 下载 nginx   nginx.org 2. 下载 php  windows.php.net   选择 nts 版本,解压后,将php.ini.development 重命名为  php.in ...

  5. jQuary总结11:jQuery插件封装---jQuery封装 手风琴 动画插件

    完整代码下载点击我的GitHub: https://github.com/XingJYGo/jquery-accordion 1 手风琴的效果展示如下: 2 封装插件目录结构如下: 主要包括:HTML ...

  6. Web大文件(夹)上传(断点续传)控件发布-Xproer.HttpUploader6

    版权所有 2009-2017荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...

  7. CVPR,ICCV和ECCV,计算机视觉三大顶级盛会

    简介:https://blog.csdn.net/hualitlc/article/details/11099693 近几年ICCV,CVPR,和ECCV论文列表:https://www.xuebuy ...

  8. WebApi跨域请求

    在实际开发中 会有提供webapi给前端js 直接调用的情况, 这时候就会有存在跨域的情况, 解决方案: 在Global中添加代码 protected void Application_BeginRe ...

  9. Android 画指南针

    1.无意看到了一个指南针的UI,在这里简单的模仿了一下.其实就是第画布的一些变化而已. 别人的效果图是: 3.简单说一下思路: 1)首先是画一个黑色圆盘 2) 然后画圆盘上的刻度(就是对Canvas一 ...

  10. 把windows电脑变成路由器使用

    实用小技巧1 把windows电脑变成路由器使用 适用对象: windows7.windows8的笔记本电脑或者有无线网卡的台式电脑 网络要求: CMCC-EDU和家里拨号上网的都可以,但是电信的校园 ...