Highway Project ZOJ - 3946 最短路

本文介绍了一种在给定城市和高速公路网络中寻找从首都到其他城市最优路径和最小总成本的方法。通过使用最短路径算法,同时考虑时间和建设成本,以实现整个帝国交通网络的最优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 Tindicating 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

思路:利用最短路求解总共需求的最短时间,同时维护第二属性建边花费(只要维护紧邻的那一条边即可)

//#include<bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define sdddd(x,y,z,k) scanf("%d%d%d%d", &x, &y, &z, &k)
#define sddd(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define sdd(x,y) scanf("%d%d", &x, &y)
#define sd(x) scanf("%d", &x)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
//#define mp make_pair
#define pb push_back
#define ms(x, y) memset(x, y, sizeof x)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const int maxn = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
typedef vector<ll> vec;
typedef vector<vec> mat;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
struct Edge {
	int to;
	ll len;
	ll cost;
	Edge() {};
	Edge(int t, ll l, ll c) :to(t), len(l), cost(c) {}
};
bool operator < (Edge a, Edge b) {
	if (a.len == b.len)
		return a.cost > b.cost;
	return a.len > b.len;
}
int path[maxn];
ll dist[maxn];
ll cist[maxn];
int vis[maxn];
vector<Edge> G[maxn];
int n, m, t;
void dij() {
	priority_queue<Edge> pque;
	pque.push(Edge(0, 0, 0));
	dist[0] = 0;
	cist[0] = 0;
	while (pque.size()) {
		Edge nn = pque.top(); pque.pop();
		if (nn.len > dist[nn.to]) continue;
		for (int i = 0; i < G[nn.to].size(); i++) {
			Edge e = G[nn.to][i];
			if (dist[e.to] > e.len + nn.len) {
				dist[e.to] = e.len + nn.len;
				path[e.to] = nn.to;
				cist[e.to] = e.cost;
				pque.push(Edge(e.to, dist[e.to], cist[e.to]));
			}
			else if (dist[e.to] == e.len + nn.len) {
				cist[e.to] = min(cist[e.to], e.cost);
			}
		}
	}
}
int main()
{
	//std::ios::sync_with_stdio(false);
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	cin >> t;
	int ta, tb, tc, td;
	while (t--) {
		ms(path, -1);
		ms(dist, 0x3f);
		ms(cist, 0x3f);
		ms(vis, 0);
		cin >> n >> m;
		rep(i, 1, m) {
			cin >> ta >> tb >> tc >> td;
			G[ta].pb(Edge(tb, tc, td));
			G[tb].pb(Edge(ta, tc, td));
		}
		dij();
		ll ansa = 0; ll ansb = 0;
		vis[0] = 1;
		rep(i, 1, n - 1) {
			ansa += dist[i];

			ansb += cist[i];
		}
		cout << ansa << " " << ansb << endl;
		rep(i, 0, n - 1)
			G[i].clear();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值