ZOJ

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

题解:我们取得第一优先级是时间,其次是花费,但是更新花费时会出现重复的,我们的思路就变成了只有在中转点的路径才更新花费,只更新新增加的一部分,这是重要的点,如果距离相等,我们看直接的花费少还是有中转点花费少

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#define INF 9999999999999
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
int n,m;
ll ttime[maxn],cost[maxn];
struct node
{
    ll to,cost,time;
    bool friend operator < ( node x,node y )
    {
    	return x.time>y.time;
    }
};
vector<node>vec[maxn];
void init()
{
	for(int t=0;t<n;t++ )
	{
		vec[t].clear();
	}
}
void Addedge()
{
	int u,v,time,cost;
	for(int t=0;t<m;t++)
	{
		scanf("%lld%lld%lld%lld",&u,&v,&time,&cost);
		node Map1,Map2;
		Map1.to=u;
		Map1.time=time;
		Map1.cost=cost;
		Map2.to=v;
		Map2.time=time;
		Map2.cost=cost;
		vec[Map1.to].push_back(Map2);
		vec[Map2.to].push_back(Map1);
		
	}
}
void Dij(int start)
{
	for(int t=0;t<n;t++ )
	{
		ttime[t]=INF;
		cost[t]=INF;
	}
	ttime[start]=0;
	cost[start]=0;
	node sta;
	sta.to=start;
	sta.cost=0;
	sta.time=0;
	priority_queue<node>q;
	q.push(sta);
	while(!q.empty())
	{
		node now=q.top();
		q.pop();
		int to=now.to;
		for(int t=0;t<vec[to].size();t++)
		{
			node temp;
			temp=vec[to][t];
			if(temp.time+ttime[now.to]<ttime[temp.to])
		    {
		    	ttime[temp.to]=temp.time+ttime[now.to];
		    	cost[temp.to]=temp.cost;
		    	node next;
		    	next.to=temp.to;
		    	next.time=ttime[temp.to];
		    	next.cost=cost[temp.to];
		    	q.push(next);
		    }
		    else if(temp.time+ttime[now.to]==ttime[temp.to])
		    {
		    	cost[temp.to]=min(cost[temp.to],temp.cost);
		    }
		}
	}
	
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		init();
		cin>>n>>m;
		Addedge();
		Dij(0);
		ll sum1=0,sum2=0;
		for(int t=0;t<n;t++)
		{
			sum1+=ttime[t];
		}
		for(int j=0;j<n;j++)
		{
			sum2+=cost[j];
		}
		cout<<sum1<<" "<<sum2<<endl;
		
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Staceyacm/p/10781748.html