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. Oracle to_char函数的使用方法

    Oracle to_char函数的功能是将数值型或者日期型转化为字符型,下面就为您详细介绍Oracle to_char函数的使用,希望对您能有所帮助. Postgres 格式化函数提供一套有效的工具用 ...

  2. 【Android优化篇】提升Activity加载速度的方法

    文章转自:http://www.jianshu.com/p/2007ca0290d3 作者: CoderFan 前言 这个也是我面试遇到的问题,当时只回答了一种情况,异步加载数据,没想到别的方式,回来 ...

  3. instanceof 和 typeof

    instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链 function Person(){ Person.prototype.dan ...

  4. centos vncviewer

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

  5. Android.PublishApplication

    发布应用 3. 为App签名 Android 要求App在安装前,需要使用证书(certificate)来进行数字签名(be digitally signed). Android 用证书来标识一个Ap ...

  6. C#泛型的学习

    编码: class Program { static void Main(string[] args) { ; Test<int> test1 = new Test<int>( ...

  7. UIDatePicker 时间选择器

    NSDate *currentTime = [NSDate date]; datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, ...

  8. java的nio例子

    package main; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.Inet ...

  9. Visual Studio Plus 开发

    参考文档:Developing Visual Studio Extensions http://msdn.microsoft.com/en-us/library/dd885119(v=vs.120). ...

  10. C# 单例模式的五种写法

    1.简单实现           C#   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public sealed c ...