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年浙江省 ...
随机推荐
- 彻底理解Toast原理和解决小米MIUI系统上没法弹Toast的问题
1.Toast的基本使用 Toast在Android中属于系统消息通知,用来提示用户完成了什么操作.或者给用户一个必要的提醒.Toast的官方定义是这样的: A toast provides simp ...
- Scala集合操作
大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka. ...
- [转]easyui 全部图标
原文地址:http://blog.163.com/shexinyang@126/blog/static/136739312201491011492263/ 拿jquery-easyui-1.2.6举例 ...
- java设计模式(八) 适配器模式
[适配器模式]将一个类的接口,转换成客户期望的另外一个接口.适配器让原本接口不兼容的类可以合作无间. 1,Duck接口 package com.pattern.adapter; public inte ...
- 【BZOJ 4455】【UOJ #185】【ZJOI 2016】小星星
http://www.lydsy.com/JudgeOnline/problem.php?id=4455 http://uoj.ac/problem/185 有一个$O(n^n)$的暴力,放宽限制可以 ...
- 犀牛书的实例代码:给对象添加freeze, hide, 查询descriptors
/* * Define a properties() method in Object.prototype that returns an * object representing the name ...
- fiddler使用教程
转载地址:写得很不错的fildder教程 http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站: www.fiddler2.c ...
- .VDI manual Technical Logistics - Volume 2: Industrial Trucks
VDI manual Technical Logistics - Volume 2: Industrial Trucks Name Publication date: State VDI 2196 B ...
- vim选中字符复制/剪切/粘贴
转载自:http://www.cnblogs.com/luosongchao/p/3193153.html 问题描述: vim 中选中指定字符,进行复制/剪切/粘贴 选择:1.普通模式下--v+hjk ...
- MVC3中使用RadioButtonFor()
创建页面 进行初始化 默认 男 被选中 <div class="label"> <div class="editor-label"> ...