ZOJ - 3946-Highway Project(最短路变形+优先队列优化)
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(最短路变形+优先队列优化)的更多相关文章
- zoj 3946 Highway Project(最短路 + 优先队列)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- 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 ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- 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> ...
- Gym 101873C - Joyride - [最短路变形][优先队列优化Dijkstra]
题目链接:http://codeforces.com/gym/101873/problem/C 题意: 这是七月的又一个阳光灿烂的日子,你决定和你的小女儿一起度过快乐的一天.因为她真的很喜欢隔壁镇上的 ...
- ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP
K - Watermelon Full of Water Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld &am ...
随机推荐
- code1225 八数码Bfs
Bfs搜索 1.把棋盘直接作为状态: #include<iostream> #include<cstring> #include<queue> #include&l ...
- Java操作XML的工具:JAXB
JavaArchitecture for XML Binding (JAXB) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例文档反向 ...
- JS和DOM的关系
DOM对象 DOM实际上是以面向对象方式描述的文档模型.DOM定义了表示和修改文档所需的对象.这些对象的行为和属性以及这些对象之间的关系. 根据W3C DOM规范,DOM是HTML与XML的应用编程接 ...
- 设计模式20:Memento 备忘录模式(行为型模式)
Memento 备忘录模式(行为型模式) 对象状态的回溯 对象状态的变化无端,如何回溯.恢复对象在某个点的状态? 动机(Motivation) 在软件构建过程中,某些对象的状态在转换过程中,可能由于某 ...
- top命令查看进程列表
top命令查看进程列表 top命令是linux下常用的性能分析工具,能实时显示系统中各个进程的资源占用状况.和win的资源管理器类似.top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态,如 ...
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...
- Window-document-javascript
一个浏览器窗口有一个window对象,javascript属于window对象,html为document对象,属于window,body为document对象,只有设置body对象高度为100%,其 ...
- 面试题: 已知一个含有n个不同元素的集合,要求打印其所有具有k个元素的子集(不允许有重复的)
TX面试题2: 已知一个含有n个元素的集合,要求打印其所有具有k个元素的子集(不允许有重复的) 题目分析, 为了便于说明,不妨将问题简化一下: 已知一个盒子中有n个不同的球,分别标记为{a1,a2,. ...
- [SIP01]SIP Header Fields里面各字段用途
INVITE sip:bob@biloxi.com SIP/2.0 Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds Max-Forw ...
- SOLR企业搜索平台 二 (分词安装)
标签:linux lucene 分词 solr 全文检索 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://3961409.blog ...