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. PC初始化

    @charset "utf-8"; /* CSS Document */ html{width:%;font-family: ;padding: ;} a{color:#;text ...

  2. angular2.0学习笔记6.编程风格指南

    1.组件的类名应该是大驼峰形式,并且以Component结尾. 因此英雄详情组件的类名是HeroDetailComponent. 组件的文件名应该是小写中线形式,每个单词之间用中线分隔,并且以.com ...

  3. JVM 堆参数调优 (四)

    堆参数调优 1.堆的结构 JAVA7 堆逻辑上分为:新生区.养老区.永久区:实际上堆只有新生区.养老区: Minor GC:轻量的垃圾回收:   Major GC(Full GC):重量级垃圾回收. ...

  4. xml配置sql语句

  5. 爬虫初窥day3:BeautifulSoup

    信息提取 1.通过Tag对象的属性和方法 #!/usr/bin/python # -*- coding: utf- -*- from urllib.request import urlopen fro ...

  6. flask部署

    https://blog.csdn.net/zhuod/article/details/77850783

  7. NETSHARP微信开发说明

    一.微信开发介绍 1.微信分为个人号,订阅号.服务号,需要去理解三个号的区别,对于开发来说也需要了解不同的账号所提供的功能 2.微信号需要审批,审批之后有一些功能才能使用 3.微信提供的功能及使用情况 ...

  8. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  9. Find the location of libmysqlclient.so.X file in Linux environments

    I'm putting together a script that has a requirement of knowing libmysqlclient.so.[15|16|18] .so fil ...

  10. 别人的Linux私房菜(1)计算机概论

    计算机主板 早期两个网桥控制通信,北桥连接速度比较快的CPU.内存.显卡.南桥连接较慢的接口,如硬盘,USB,网卡等.北桥的控制器集成到了CPU中. CPU工作频率 外频:CPU与外部组件进行数据传输 ...