ZOJ Problem Set - 3946 (限制的最短路)


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

题意:一个帝国有 n 个城市,可以在城市两两之间建立 m 条高速公路,建立  x-y

           之间的高速路需要时间 d,花费为 c,最后建立完边(<=m)后使得首都 0

           号城市到各个城市(1--n-1)的总时间最少,在多个时间满足条件下再选花

           费最少的。

分析:乍一看一开始以为是最小生成树,结果连事例都没出,后来发现是最短路加上

            限制就搞定。首要条件是花费时间最短,所以建完双向边后跑下 spfa 就可以,

            只是当分别加入多个点都能使到达某点 a 的时间都最短时判断加入哪个点能使得

            这条路费用最少,再将此点到上述的某点 a 的时间累加存放到 tt[a],将此点到 a

            的费用 c 存放至点 a 即 cost[a]。最后将每个点的时间 tt[i] 累加,将每个点的花费

            cost[i] 累加即可得到答案。

注意:用前向星建边会出现 RE 和 SE 越界错误,数组大小难确定,我后来直接开了 5 倍过了!

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
#define N 50005 * 5
int vis[N], head[N], k;
ll tt[N], cost[N];
struct r
{
	int v, next;
	ll t, c;
}p[N];
void add(int u, int v, int t, int c)//前向星建边
{
	p[k].v = v;
	p[k].t = t;
	p[k].c = c;
	p[k].next = head[u];
	head[u] = k++;
}
void spfa()
{
	memset(cost, inf, sizeof(cost));
	memset(tt, inf, sizeof(tt));
	memset(vis, 0, sizeof(vis));
	queue<int >q;
	q.push(0);
	vis[0] = 1;
	cost[0] = tt[0] = 0;
	while (!q.empty()){
		int out = q.front(); q.pop();
		vis[out] = 0;
		for (int i = head[out]; i + 1; i = p[i].next){
			if (tt[out] + p[i].t <= tt[p[i].v]){
				if (tt[out] + p[i].t < tt[p[i].v]){
					tt[p[i].v] = tt[out] + p[i].t;
					cost[p[i].v] = p[i].c;
					if (!vis[p[i].v]){
						q.push(p[i].v);
						vis[p[i].v] = 1;
					}
				}
				else if (cost[p[i].v]>p[i].c) {//时间相同考虑费用小的边
					if (!vis[p[i].v]){
						q.push(p[i].v);
						vis[p[i].v] = 1;
					}
					cost[p[i].v] = p[i].c;
				}
			}
		}
	}
}
int main()
{
#ifdef OFFLINE
	freopen("t.txt", "r", stdin);
#endif
	int t, i, j, n, m, x, y, d, c;
	scanf("%d", &t);
	while (t--){
		k = 0;
		memset(head, -1, sizeof(head));
		scanf("%d%d", &n, &m);
		for (i = 0; i < m; i++){
			scanf("%d%d%d%d", &x, &y, &d, &c);
			add(x, y, d, c);//双向边
			add(y, x, d, c);
		}
		spfa();
		ll d = 0, c = 0;
		for (i = 0; i < n; i++){
			d += tt[i];
			c += cost[i];
		}
		printf("%lld %lld\n", d, c);//爆 int 用 ll
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值