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年浙江省 ...
随机推荐
- Linux进程间通信之管道
1,进程间通信 (IPC ) Inter-Process Communication 比较好理解概念的就是进程间通信就是在不同进程之间传播或交换信息. 2,linux下IPC机制的分类:管道.信号.共 ...
- express-partials与express4.x不兼容问题
在express中设置view engine为html,express-partials会导致语法不正确,其实只要做一行代码的改动就可以 function renderer(ext){ if(ext[ ...
- 出现“System.Data.SqlClient.SqlError: 尚未备份数据库的日志尾部”错误的解决方案
Sql Server2008数据库在还原时出现如下错误信息:System.Data.SqlClient.SqlError: 尚未备份数据库<数据库名称>的日志尾部.如果该日志包含您不希望丢 ...
- CMD命令简单使用
1.定位磁盘 2.打开文件路径 3.查看文件目录里面所有的文件或目录信息
- java swing模仿随机频谱
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Grap ...
- STL中的next_permutation
给定一个数组a[N],求下一个数组. 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 ..... 在STL中就有这个函数: 1.参数是(数组的第一个元素,数组的末尾),注意这是前闭后开 ...
- MyBatis学习总结
1.引入jar包到lib目录下:只需要mybatis的一个mybatis.jar及数据库的jar包. 2.在src下新建xml配置文件,即上图中的conf.xml <?xml version=& ...
- git log 格式化输出
Git log --graph --pretty=format: '%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)& ...
- 【BZOJ 2599】【IOI 2011】Race 点分治
裸的点分治,然而我因为循环赋值$s$时把$i <= k$写成$i <= n$了,WA了好长时间 #include<cstdio> #include<cstring> ...
- <UL>中<li>标签前编号图片的简单调用
<style type="text/css"> ul li{ list-style-type:none} .men ul{ background:url(http:// ...