[Dijkstra] Highway Project 16浙江省赛K

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 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).

<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;
 } 

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/ummmmm/article/details/80071499
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞