The 13th Zhejiang Provincial Collegiate Programming Contest - K Highway Project

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

题意:从0点出发去建路,使得0点到其他全部的时间最小,如果最小时间出现重复就选择花费最小的路;

思路:也算是最短路的模板题,就不过是在求最短路的过程中,如果遇到从0点出发到当前的点的时间出现相同时,比较之前的花费和现在的花费取最小的,还有答案可能爆int的,所以应该使用long long;

代码:

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cmath>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<set>  
#include<queue>  
#include<string>  
#include<bitset>  
#include<utility>  
#include<functional>  
#include<iomanip>  
#include<sstream>  
#include<ctime>  
using namespace std;  

#define N int(1e5+10)  
#define inf int(0x3f3f3f3f)  
#define mod int(1e9+7)  
typedef long long LL;  

struct point
{
	int x;
	LL cost,time;
	point(int _x,LL _c,LL _t):x(_x),time(_c),cost(_t){}
};

vector< vector<point> >g;
LL vis[N],d[N],cost[N];
void spfa()
{
	memset(d,inf,sizeof(d));
	memset(cost,inf,sizeof(cost));
	memset(vis,0,sizeof(vis));
	vis[0] = 1;
	d[0] = 0;
	cost[0] = 0;
	queue<int>q;
	q.push(0);
	while(!q.empty())
	{
		int x = q.front(); q.pop();
		vis[x] = 0;
		for(int i = 0; i<g[x].size(); i++)
		{
			int v = g[x][i].x;
			if(v == x) continue;
			LL Time = g[x][i].time;
			LL Cost = g[x][i].cost;
			if(d[v] > d[x] + Time)
			{
				d[v] = d[x] + Time;
				cost[v] = Cost;
				if(!vis[v]){
					q.push(v);
					vis[v] = 1;
				}
			}
			else if(d[v] == d[x] + Time)//如果最小时间出现相同,取最小的花费
			{
				cost[v] = min(cost[v],Cost);
			}
		}
	}
}

int main()  
{  
#ifdef CDZSC_June  
	freopen("t.txt", "r", stdin);  
	int _time_jc = clock();  
#endif  
	int t1,n,m,x,y;
	LL sum1,sum2,t,c;
	scanf("%d",&t1);
	while(t1--)
	{
		sum1 = sum2 = 0;
		g.clear();
		scanf("%d%d",&n,&m);
		g.resize(n+10);
		for(int i = 0; i<m ; i++)
		{
			scanf("%d%d%lld%lld",&x,&y,&t,&c);
			g[x].push_back(point(y,t,c));
			g[y].push_back(point(x,t,c));//添加反向边
		}
		spfa();
		for(int i = 0; i<n ; i++)
		{
			sum1 += d[i];
			sum2 += cost[i];
		}
		printf("%lld %lld\n",sum1,sum2);
	}
	return 0;  
}  



















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值