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 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 题目大意:
给你 T 组测试数据, 每组测试数据有个 n 和 m,表示有 n 个点 m 条边,这 m 条边分别有它修建的价值和从这条边上通过的时间,现在问题来了, 问你如何修建能够让它需要的时间最小, 在时间最小的前提下, 让修路花费的时间也尽可能的小, 最后求从 0 点到各个点的总时间和建路花费的费用

先将起始点加到队列里面, 然后访问起始点能够到达的点把满足要求的点在加到队列里面, 依次直到队列里面没有点了, 就结束, 此时dist里面存的值,就是自己想要的值

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std; typedef long long LL;
#define N 110000
#define met(a,b) (memset(a,b,sizeof(a)))
const LL INF = (1ll<<)-; struct node
{
LL v, cost, time, next;
}a[N<<]; LL Head[N], cnt, sumc, sumt, distc[N], distt[N];
int n, m, vis[N]; void Init()
{
cnt = ;
met(Head, -);
}
void Add(int u, int v, int cost, int time)
{
a[cnt].v = v;
a[cnt].cost = cost;
a[cnt].time = time;
a[cnt].next = Head[u];
Head[u] = cnt++; swap(u, v); a[cnt].v = v;
a[cnt].cost = cost;
a[cnt].time = time;
a[cnt].next = Head[u];
Head[u] = cnt++;
} void spfa()
{
int u, v, cost, time, i;
met(vis, );
vis[] = ; for(i=; i<n; i++)
{
distt[i] = INF;
distc[i] = INF;
}
distt[] = distc[] = ; queue<int>Q;
Q.push(); while(Q.size())
{
u = Q.front(), Q.pop(); for(i=Head[u]; i!=-; i=a[i].next)
{
v = a[i].v;
cost = a[i].cost;
time = a[i].time; if((distt[v]>distt[u]+time)||(distt[v]==distt[u]+time && distc[v]>cost))
{
distt[v] = distt[u]+time;
distc[v] = cost; if(!vis[v])
{
vis[v] = ;
Q.push(v);
}
}
}
vis[u] = ;
} sumc = sumt = ; for(i=; i<n; i++)
{
sumc += distc[i];
sumt += distt[i];
}
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int i;
LL u, v, cost, time; Init(); scanf("%d%d", &n, &m); for(i=; i<=m; i++)
{
scanf("%lld%lld%lld%lld", &u, &v, &time, &cost);
Add(u, v, cost, time);
} spfa(); printf("%lld %lld\n", sumt, sumc);
}
return ;
}

(spfa) Highway Project (zoj 3946 )的更多相关文章

  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(Dijkstra)

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

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

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

  5. ZOJ 3946 Highway Project

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

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

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

  7. ZOJ 3946 Highway Project (最短路)

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

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

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

  9. ZOJ - 3946-Highway Project(最短路变形+优先队列优化)

    Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...

随机推荐

  1. node.js下载安装

    1.下载node.js在node中文网站,官方网站下载太慢 2.接着让我们点击下载链接,页面上呈现出你所需要下载的安装包,我们这里选择windows x64的安装包进行下载 3.安装node.js,一 ...

  2. CSS背景background图片

    一.CSS背景background图片   -   TOP 1.背景图片语法background-image:url() 引入背景图片background-repeat:no-repeat 设置背景图 ...

  3. linux操作系统-设置静态ip

    在使用linux虚拟机的时候因为经常有关机的需求,然后重新开机后可能面临这上一次获取的ip被改变,在这里我分享一下在linux 下设置静态ip的经验 1.查看路由状态 [root@localhost ...

  4. c#异步等待

    1:一直等待 2方法  现实等待 3:方法    带返回值得 begin -endinvoke 4: func 带返回值

  5. 10.Mysql索引

    10.索引的设计和使用10.1 索引概述BTREE索引:Mysql(MyIASM和Innodb)默认的索引类型.前缀索引:对索引字段的前N个字符创建索引.N的最大取值和存储引擎有关,MyIASM支持最 ...

  6. centos vncviewer

    CentOS6.5 安装vncserver实现图形化访问   一. 安装gnome图形化桌面 #yum groupinstall -y "X Window System" #yum ...

  7. BZOJ 1969 航线规划 - LCT 维护边双联通分量

    Solution 实际上就是查询 $u$ 到 $v$ 路径上 边双的个数 $ -1$. 并且题目仅有删边, 那么就离线倒序添边. 维护 边双 略有不同: 首先需要一个并查集, 记录 边双内的点. 在 ...

  8. php7.0-fpm.sock

    .sock  这种方式是套接字的方式连接的

  9. [AI]AI章1 框架选型

    工欲善其事,必先利其器 你想选哪个? 如何选?先来介绍下,也许有帮助... 介绍下几个名词: 卷积神经网络(Convolutional Neural Network,CNN) CNN是一种前馈神经网络 ...

  10. 2016-2017-2 20155312 实验三敏捷开发与XP实践实验报告

    1.研究code菜单 Move Line/statement Down/Up:将某行.表达式向下.向上移动一行 suround with:用 try-catch,for,if等包裹语句 comment ...