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. Linux 任务管理 && 常用指令

    A.linux死机 转自:https://www.deleak.com/blog/2010/10/20/sysrq/ linux死机了怎么办? 曾经啊,对着键盘上 Print Screen/SysRq ...

  2. Vue 安装脚手架 工具 vue-cli (最新)

    假如您安装过旧版脚手架工具(vue-cli),您可以通过 npm uninstall vue-cli -g 或 yarn global remove vue-cli卸载. Vue CLI 需要Node ...

  3. PAT 1008 数组元素循环右移问题 (20)(代码)

    1008 数组元素循环右移问题 (20)(20 分) 一个数组A中存有N(N&gt0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A ...

  4. hdu 2066 ( 最短路) Floyd & Dijkstra & Spfa

    http://acm.hdu.edu.cn/showproblem.php?pid=2066 今天复习了一下最短路和最小生成树,发现居然闹了个大笑话-----我居然一直写的是Floyd,但我自己一直以 ...

  5. Java 中转换为String类型的四种方法

    1. 使用 String 的构造方法,用于 byte[], char[], StringBuffer, StringBuilder 类型 2. 使用 String 的静态方法 valueOf() 推荐 ...

  6. Java中创建对象的四种方法

    第一种 使用new关键字 第二种 使用反射技术:1)通过Class类的newInstance()方法:2)通过Constructor类的newInstance方法 第三种 通过Object类的clon ...

  7. VMware Workstation 15 pro keys

    永久激活密钥UG5J2-0ME12-M89WY-NPWXX-WQH88 GA590-86Y05-4806Y-X4PEE-ZV8E0 YA18K-0WY8P-H85DY-L4NZG-X7RAD UA5D ...

  8. MySQL 快速复数据库的方法

    为了方便快速复制一个数据库,可以用以下命令将db1数据库的数据以及表结构复制到newdb数据库 创建新的数据库 #mysql -u root -p123456 mysql>CREATE DATA ...

  9. 复制粘贴容易犯的错误 eclipse

    有时候复制原有的代码到xml文件中,会提示某文件没有找到,一般该文件名字改成别的了,这时候为了解决这问题一般需要对这个文件重命名

  10. C语言基础第五次作业

    题目7-2 统计一行文本的单词个数 1.实验代码 #include <stdio.h> int main() { char a; ,countword=; ){ scanf("% ...