zoj 3946 Highway Project(最短路 + 优先队列)
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 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
题意:从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(最短路 + 优先队列)的更多相关文章
- ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...
- 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 ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- ZOJ 3946 Highway Project
1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- ZOJ - 3946-Highway Project(最短路变形+优先队列优化)
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...
- ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...
- ZOJ-3946 Highway Project (最短路)
题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...
随机推荐
- .net 估计要死在你手里了
最近不太爽,想换工作,上这些知名的招聘网站,一搜 .net 心凉了一截,很少有大公司用.net,工资也不是很高. 不用我多说什么,想必很多人应该有类似经历,只是打了牙往肚子里咽. 来两副图: 最近用滴 ...
- TCP/IP中最高大上的链路层简介(二)
引言 对于程序猿来讲,似乎越接近底层,就越显得高大上.这也算是程序猿们的共同认知吧,虽然不是所有人.今天LZ就和各位一起探讨一下TCP/IP中最高大上的一层,也就是最底层的链路层. 这一层LZ了解的还 ...
- Html.BeginForm
该方法用于构建一个From表单的开始,他的构造方法为: Html.BeginForm("ActionName","ControllerName",FormMet ...
- Excel导入导出,通过datatable转存(篇一)
//导入数据 public ActionResult ExpressInfoImport() { var ptcp = new BaseResponse() { DoFlag = true, DoRe ...
- 学习Google Protocol buffer之概述
XML这种属于非常强大的一种格式,能存储任何你想存的数据,而且编辑起来还是比较方便的.致命的缺陷在于比较庞大,在某些情况下,序列化和解析都会成为瓶颈.这种对于实时性很强的应用来说,就不太适合了,想象下 ...
- jQuery自定义插件
jQuery自定义插件 jQuery自定义插件按照功能分类,可以分为三类, 1>封装对象方法的插件,(也就是基于某个DOM元素的jQuery对象,局部的) 2>封装全局函数的插件, ( ...
- 开发错误12:gradle编译错误:Conflict with dependency com.android.support:support-annotations
在build.gradle中的configurations.all {}下添加:resolutionStrategy.force 'com.android.support:support-annota ...
- 开发错误记录9:Application无法跳转到Activity
今天在做友盟消息推送,当客户端收到推送消息时,如客户端不是活动的,用如下代码 启动Activity发现无法启动,原因是原有的任务栈已消毁,判断启动是如消毁,如有,再新建一个 只要给它添加一个标志 就可 ...
- [转]SpringMVC+Hibernate+Spring 简单的一个整合实例
原文地址:http://langgufu.iteye.com/blog/2088355 下面开始实例,这个实例的需求是对用户信息进行增删改查.首先创建一个web项目test_ssh,目录结构及需要的J ...
- python复习
1.input和raw_input的区别 input假设输入的都是合法的python表达式,如输入字符串时候,要加上引号,而raw_input都会将所有的输入作为原始数据 2.原始字符串前面加上r,e ...