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 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 NM (1 ≤ NM ≤ 105).

Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ XiYi < N, 0 < DiCi < 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

题意:从0点开始到各点距离最小,在距离最小情况下还要花费最少,做这题的时候题目没搞明白,

优先队列维护,

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
const LL INF = ((LL) ) << ;
const int Max = 1e5 + ;
struct Edge
{
int v, d, c;
int Next;
};
struct Node
{
int u;
LL d, c;
friend bool operator<(const Node & a, const Node & b)
{
if (a.d == b.d)
return a.c > b.c;
return a.d > b.d;
}
};
Node node[Max];
Edge edge[Max * ];
int head[Max];
int vis[Max];
int n, m, tot;
void addEdge(int u, int v, int d, int c)
{
edge[tot].v = v; edge[tot].d = d; edge[tot].c = c;
edge[tot].Next = head[u];
head[u] = tot++; edge[tot].v = u; edge[tot].d = d; edge[tot].c = c;
edge[tot].Next = head[v];
head[v] = tot++;
} void dijstra()
{
for (int i = ; i <= n; i++)
{
node[i].u = i;
node[i].d = INF;
node[i].c = INF;
}
priority_queue<Node> que;
node[].c = node[].d = ;
que.push(node[]);
memset(vis, , sizeof(vis)); while (!que.empty())
{
Node temp = que.top();
que.pop();
int u = temp.u;
if (vis[u])
continue;
vis[u] = ;
for (int i = head[u]; i != -; i = edge[i].Next)
{
if (vis[ edge[i].v ])
continue;
if (node[ edge[i].v ].d > node[ u ].d + edge[i].d)
{
node[ edge[i].v ].d = node[ u ].d + edge[i].d;
node[ edge[i].v ].c = edge[i].c;
que.push(node[ edge[i].v ]);
}
else if (node[ edge[i].v ].d == node[ u ].d + edge[i].d && node[ edge[i].v ].c > edge[i].c)
{
node[ edge[i].v ].c = edge[i].c;
que.push(node[ edge[i].v ]);
}
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
int u, v, d, c;
tot = ;
for (int i = ; i <= m; i++)
{
scanf("%d%d%d%d", &u, &v, &d, &c);
addEdge(u, v, d, c);
}
dijstra();
LL sum_d = , sum_c = ;
for (int i = ; i < n; i++)
{
sum_d += node[i].d;
sum_c += node[i].c;
}
printf("%lld %lld\n", sum_d, sum_c);
}
return ;
}

zoj 3946 Highway Project(最短路 + 优先队列)的更多相关文章

  1. ZOJ 3946 Highway Project (最短路)

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

  2. 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 ...

  3. ZOJ 3946 Highway Project(Dijkstra)

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

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

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

  5. ZOJ 3946 Highway Project

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

  6. (spfa) Highway Project (zoj 3946 )

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718   Highway Project Time Limit: 2 Seco ...

  7. ZOJ - 3946-Highway Project(最短路变形+优先队列优化)

    Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...

  8. ZOJ3946:Highway Project(最短路变形)

    本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...

  9. ZOJ-3946 Highway Project (最短路)

    题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...

随机推荐

  1. Bootstrap系列 -- 43. 固定导航条

    很多情况之一,设计师希望导航条固定在浏览器顶部或底部,这种固定式导航条的应用在移动端开发中更为常见.Bootstrap框架提供了两种固定导航条的方式:  .navbar-fixed-top:导航条固定 ...

  2. 路由信息协议(RIP)的防环机制

    防环机制 1-记数最大值(maximum hop count):定义最大跳数(最大为15跳),当跳数为16跳时,目标为不可达. 2-水平分割(split horizon):从一个接口学习到的路由不会再 ...

  3. 转:PHP中防止SQL注入的方法

    [一.在服务器端配置] 安全,PHP代码编写是一方面,PHP的配置更是非常关键. 我们php手手工安装的,php的默认配置文件在 /usr/local/apache2/conf/php.ini,我们最 ...

  4. php - 上传图片之痛(建文件夹)

    $json_result ['status'] = 0; $path = '../upfile'; $json_result ['status'] = 0; $json_result ['succes ...

  5. "此站点已经禁用应用程序"在sharepoint 2013中通过v2013部署app提示该错误

    该错误的原文是:the apps are disabled in this site 可以在yahoo或者bing上搜索这个错误,可以找到解决办法: msdn上也有该错误解决办法,但是如果搜索中文,目 ...

  6. .NET中的GDI+

    GDI:Graphics Device Interface. System. Windows. Shapes 命名空间: 类 Ellipse 绘制一个椭圆. Line 在两个点之间绘制一条直线. Pa ...

  7. RAP在centos上的部署

    在centos7上部署RAP(非官方) 作者批注:该部署文档为网友贡献,仅供参考.war请参考主页README.md下载最新版本哟~~~ 感谢热情网友的Wiki整理!万分感谢! 系统: centos7 ...

  8. CMY/CMYK 打印机色彩

    CMY 发光物体和反光物体产生颜色的机制不同. 前者指光源光, 它的颜色由发光波长决定. 后者指不能发光但却能表现出颜色的物体, 例如色素. 色素的颜色由它不能吸收的光的波长决定. 比如红色色素, 除 ...

  9. JavaScript写一个拼图游戏

    拼图游戏的代码400行, 有点多了, 在线DEMO的地址是:打开: 因为使用canvas,所以某些浏览器是不支持的: you know: 为什么要用canvas(⊙o⊙)?  因为图片是一整张jpg或 ...

  10. Maven-pom.xml详解

    (看的比较累,可以直接看最后面有针对整个pom.xml的注解) pom的作用 pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵 ...