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 NM (1 ≤ NM ≤ 105).

Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ XiYi < N,
0 < DiCi < 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)的更多相关文章

  1. 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 ...

  2. zoj 3946 Highway Project(最短路 + 优先队列)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...

  3. ZOJ 3946 Highway Project 贪心+最短路

    题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...

  4. ZOJ 3946 Highway Project

    1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...

  5. ZOJ 3946 Highway Project (最短路)

    题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...

  6. (spfa) Highway Project (zoj 3946 )

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5718   Highway Project Time Limit: 2 Seco ...

  7. ZOJ3946:Highway Project(最短路变形)

    本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...

  8. ZOJ-3946 Highway Project (最短路)

    题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...

  9. The 13th Zhejiang Provincial Collegiate Contest(2016年浙江省赛)

      前4道水题就不说了,其中我做了C题,1Y,小心仔细写代码并且提交之前得确认无误后提交才能减少出错率. 结果后面2题都由波神做掉,学长带我们飞~ 终榜 官方题解   ZOJ 3946 Highway ...

随机推荐

  1. Jekins部署.net站点

    前提 1.你需要一台windows服务 可以装vs的且有重启电脑权限的(具体vs版本根据你的团队决定) 2.下载jekins 安装包 地址:https://jenkins.io/download/   ...

  2. Linux - 进程控制 代码(C)

    进程控制 代码(C) 本文地址:http://blog.csdn.net/caroline_wendy 输出进程ID.getpid(). 代码: /*By C.L.Wang * Eclipse CDT ...

  3. Lintcode---二叉树的层次遍历(原型)

    给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 您在真实的面试中是否遇到过这个题? Yes 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 ...

  4. [Android] 通过Menu实现图片怀旧、浮雕、模糊、光照和素描效果

        因为随手拍项目想做成类似于美图秀秀那种底部有一排Menu实现不同效果的功能,这里先简介怎样通过Menu实现打开相冊中的图片.怀旧效果.浮雕效果.光照效果和素描效果.后面可能会讲述怎样通过Pop ...

  5. atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx

    atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx 1. 国民财富的性质和原因的研究(简称:<国富论>) 1 2. 蕴含的重要管理 ...

  6. linux下创建用户(转)

    转自 http://www.cnblogs.com/ylan2009/articles/2321177.html Note: 1, Linux Shell 按Tab键不能补全 发现使用新增的用户登陆的 ...

  7. Java 堆内存

    堆内存 Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ...

  8. Mybatis设置超时时间

    Mybatis设置超时时间 mybatis如果不指定,默认超时时间是不做限制的,默认值为0.mybatis sql配置超时时间有两种方法: 1.全局配置 在mybatis配置文件的settings节点 ...

  9. windows下安装vundle

    windows下安装vundle ## 前言 windows下安装vundle和linux下稍微有些不一样,虽然官网给出了 安装说明,但是有些问题的. E117: Unknown function: ...

  10. Xcode 10 关于 CocoaPods 安装失败的问题RuntimeError

    xcode 10的情况下执行pod install报错了 RuntimeError - [!] Xcodeproj doesn't know about the following attribute ...