ZOJ 3946 Highway Project(Dijkstra)
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 cityXi 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 Dijkstra 用优先队列 ,用邻接表建立图,注意要long long int。比赛最后一分钟发现数组没有long long int. 还有在更新s数组的 要加等号,#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <string>
#include <stdio.h>
#include <queue> using namespace std;
const long long int INF=1000000000000000;
#define MAX 100000
struct Node
{
int value;
int next;
long long int time;
long long int cost;
}edge[MAX*2+5];
struct Node2
{
int time;
int cost;
int value;
Node2(){};
Node2(int time,int cost,int value)
{
this->time=time;
this->cost=cost;
this->value=value;
}
friend bool operator<(Node2 a,Node2 b)
{
if(a.time==b.time)
return a.cost>b.cost;
return a.time>b.time;
}
};
int cot;
int head[MAX+5];
int vis[MAX+5];
long long int s[MAX+5];
long long int ans1;
long long int ans2;
int n,m;
void add(int x,int y,long long int time,long long int cost)
{
edge[cot].value=y;
edge[cot].next=head[x];
edge[cot].time=time;
edge[cot].cost=cost;
head[x]=cot++;
}
void Dijkstra()
{
priority_queue<Node2> q;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
s[i]=INF;
s[0]=0;
q.push(Node2(0,0,0));
while(!q.empty())
{
Node2 term=q.top();
q.pop();
if(vis[term.value]) continue;
vis[term.value]=1;
ans2+=term.cost;
int a=head[term.value];
while(a!=-1)
{
if(s[edge[a].value]>=s[term.value]+edge[a].time)
{
s[edge[a].value]=s[term.value]+edge[a].time;
q.push(Node2(s[edge[a].value],edge[a].cost,edge[a].value));
}
a=edge[a].next;
}
}
}
int main()
{
int t;
scanf("%d",&t);
int x,y;
long long int xx,yy;
while(t--)
{
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
cot=0;
for(int i=1;i<=m;i++)
{
scanf("%d%d%lld%lld",&x,&y,&xx,&yy);
add(x,y,xx,yy);
add(y,x,xx,yy);
}
ans1=0;ans2=0;
Dijkstra();
for(int i=1;i<n;i++)
ans1+=s[i];
printf("%lld %lld\n",ans1,ans2); }
return 0;
}
ZOJ 3946 Highway Project(Dijkstra)的更多相关文章
- 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(最短路 + 优先队列)
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> ...
- ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...
- (spfa) Highway Project (zoj 3946 )
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718 Highway Project Time Limit: 2 Seco ...
- 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年浙江省 ...
- The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)
前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解 ZOJ 3946 Highway ...
随机推荐
- C# -- 使用递归列出文件夹目录及目录下的文件 神技do{}while(false)
C# -- 使用递归列出文件夹目录及目录下的文件 使用递归列出文件夹目录及目录的下文件 1.使用递归列出文件夹目录及目录下文件,并将文件目录结构在TreeView控件中显示出来. 新建一个WinFor ...
- 点滴积累【JS】---JS小功能(button选择颜色)
效果: 代码: <head runat="server"> <title></title> <style type="text/ ...
- LNK2019: 无法解析的外部符号(函数实现没有加namespace前缀导致)
问题描述: 在A.h中,我写了如下函数 namespace XXX { void func(); } 在A.cpp中,我写了如下实现 #include "A.h" using na ...
- 基于部标Jt/T809协议和Java Netty框架构建Gps位置监控平台
现在地方上由于运输车辆的GPS数据都分散在地方上已有的各种企业平台上面,不利于大数据的分析和智能应用,而开发智能的基于大数据的Gps监控平台,往往需要和各种第三方的部标GPS监控平台对接,获取到第三方 ...
- hadoop节点挂死的一次分析报表。
hadoop的一个节点unused了.然后重启启动hadoop的服务,仍有有一个hadoop的节点起不来.多次重启hadoop和杀进程之后,发现hadoop的master和slave节点上的状态在切换 ...
- hdu6038 Function 函数映射
/** 题目:hdu6038 Function 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6038 题意:给定一个a排列[0,n-1],一个b排列[0, ...
- 浅谈Java中的补零扩展和补符号位扩展
今天,魏屌出了一道题,题目如下: 定义一个大头序的byte[]a={-1,-2,-3,-4},转换成short[]b.问b[0]和b[1]分别是多少? 乍一看,这题不难,无非就是移位操作,再进行组合. ...
- Xcode模拟iPhone教程!
iOS 开发者常常会使用模拟器来进行调试,当然这就少不了Mac电脑中的Xcode软件了,今天PC6小编就给大家带来在Mac系统下如何快速启动iOS模拟器的使用教程: 一.如何启动iOS模拟器 1.在L ...
- 【Java】之static静态方法与非static静态方法区别
1.A.class:没有static public class A { public String getText(){ } B.class调用A的方法时 public class B { publi ...
- Asp.Net中使用水晶报表
Asp.Net中使用水晶报表(上) 在我们对VS.Net中的水晶报表(Crystal Reports)进行研究之前,我和我朋友对如何将这个复杂的东东加入我们的Web应用有着非常的好奇心.一周以后,在阅 ...