[Dijkstra] Highway Project 16浙江省赛K

本文介绍了一个算法问题,即如何为Marjar帝国构建最优的高速公路网络,使得从首都到达其他城市的总时间最短,并且在满足此条件的基础上成本最低。通过使用Dijkstra算法,解决了这一复杂的图论问题。

摘要生成于 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 costsCi 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 NM (1 ≤ NM ≤ 105).

Then followed by M lines, each line contains four integers XiYiDiCi (0 ≤ Xi,Yi < N, 0 < DiCi < 105).

<h4< dd="">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.

<h4< dd="">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
<h4< dd="">Sample Output
4 3
4 4


#include <bits/stdc++.h>
using namespace std;
long long int d[100005];
long long int mo[100005];
//mo存到该点的最短一截路 
struct WAY
{
    int to;
    long long int cost;
    long long int fee;
};
vector<WAY>way[100005];
struct node
{
	int num;
	long long int dis;
};
bool operator <(node a,node b)
{
	return a.dis>b.dis;
}  //取距离最短的点 
priority_queue<node>que;
void dijkstra()
{
	while(!que.empty())
	{
		node now=que.top();que.pop();
		int nu=now.num;int di=now.dis;
		int L=way[nu].size();
		for(int i=0;i<L;i++)
		{
			WAY w=way[nu][i];
			if(d[w.to]>(d[nu]+w.cost))
			{
				mo[w.to]=w.fee;
				d[w.to]=d[nu]+w.cost;
				node no;
				no.num=w.to;no.dis=d[w.to];
				que.push(no);
			}
			else if(d[w.to]==(d[nu]+w.cost))
				mo[w.to]=min(mo[w.to],w.fee);
		}
	}
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		for(int i=0;i<100005;i++)
		    way[i].clear(); 
		int n,m;
		cin>>n>>m;
		while(m--)
		{
			int x,y,d,c;
			cin>>x>>y>>d>>c;
			WAY w;
			w.to=y;w.cost=d;w.fee=c;
			way[x].push_back(w);
			w.to=x;
			way[y].push_back(w);
		}
		for(int i=1;i<n;i++)
		   d[i]=1e18;
		d[0]=0;
		node w;w.num=0;w.dis=0;
		que.push(w);
		dijkstra();
		long long int sum=0;
		for(int i=0;i<n;i++)
		   sum+=d[i];
		long long int money=0;
		for(int i=1;i<n;i++)
		   money+=mo[i];
		cout<<sum<<' '<<money<<endl;
	}
	return 0;
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值