(spfa) Highway Project (zoj 3946 )
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 N, M (1 ≤ N, M ≤ 105).
Then followed by M lines, each line contains four integers Xi, Yi, Di, Ci (0 ≤ Xi, Yi < N, 0 < Di, Ci < 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 )的更多相关文章
- 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 ...
- zoj 3946 Highway Project(最短路 + 优先队列)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ 3946 Highway Project(Dijkstra)
Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the emperor of the Marjar ...
- ZOJ3946:Highway Project(最短路变形)
本文转载自:http://www.javaxxz.com/thread-359442-1-1.html Edward, the emperor of the Marjar Empire, wants ...
- ZOJ 3946 Highway Project
1.迪杰斯特拉最小堆 #include<cstdio> #include<cstring> #include<cmath> #include<map> ...
- ZOJ 3946 Highway Project 贪心+最短路
题目链接: http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946 题解: 用dijkstra跑单元最短路径,如果对于顶点v,存 ...
- ZOJ 3946 Highway Project (最短路)
题意:单源最短路,给你一些路,给你这些路的长度,给你修这些路的话费,求最短路和最小花费. 析:本质就是一个最短路,不过要维护两个值罢了,在维护花费时要维护的是该路要花多少,而不是总的路线花费. 代码如 ...
- ZOJ-3946 Highway Project (最短路)
题目大意:一张带权无向图,权有两个参数(d,c),分别表示走过这条边的时间和建造这条边的代价.要求选出一些边,使得0节点到其他点的距离之和最短,并在最短的基础上求最小代价. 题目分析:这是16年浙江省 ...
- ZOJ - 3946-Highway Project(最短路变形+优先队列优化)
Edward, the emperor of the Marjar Empire, wants to build some bidirectional highways so that he can ...
随机推荐
- PHP开发——常见问题
执行顺序 常见数据类型 字符乱码
- 16.Mysql SQL Mode
16.SQL Mode及相关问题SQL Mode定义了Mysql支持的SQL语法和数据校验级别,Mysql支持多种SQL Mode.用途: 设置不同的SQL Mode可以对数据进行不同严格程度的校验, ...
- 5C - A == B ?
Give you two numbers A and B, if A is equal to B, you should print "YES", or print "N ...
- 201621123008 《Java程序设计》第十周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...
- DNSlog盲注
前言 在渗透测试中,经常遇到如下情景: 1,sql盲注 2,blind型ssrf以及xxe 3,无回显命令执行漏洞 ... dnslog盲注原理 开放的DNSlog平台: http://ceye.io ...
- SQL Server 查询中文字段返回为空
昨晚维护系统数据时,遇到个奇怪现象.明明数据库里有数据,查询结果就是返回为空.具体情况是这样的,查询工作日志表里关于工作描述的情况,以中文内容匹配工作描述字段,其中匹配内容里包含有空格. 尝试去掉第一 ...
- 调用webservice时,产生android.os.NetworkOnMainThreadException错误
android.os.NetworkOnMainThreadException 网上搜索后知道是因为版本问题,在4.0之后在主线程里面执行Http请求都会报这个错,也许是怕Http请求时间太长造成程序 ...
- 纯H5+c3实现表单验证
客户端验证是网页客户端程序最常用的功能之一,我们之前使用了各种各样的js库来进行表单的验证.HTML5其实早已为我们提供了表单验证的功能.至于为啥没有流行起来估计是兼容性的问题还有就是样式太丑陋了吧. ...
- 20155312张竞予 20170510实践一:在IDEA中以TDD的方式对String类和Arrays类进行学习
实践题目 在IDEA中以TDD的方式对String类和Arrays类进行学习 测试相关方法的正常,错误和边界情况 String类 charAt split Arrays类 sort binarySea ...
- hdu 6073
题意: 给出一个二部图,U.V分别是二部图的两个点集,其中,U中每个点会有两条边连到V中两个不同的点. 完美匹配定义为:所有点都成功匹配. 思路:已知一定是完美匹配了呀(也一定存在),我们先把度数为一 ...