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

本文介绍了一个高速公路项目的算法问题,目标是从首都出发通过建设双向高速公路达到其他城市,寻找总时间最短且在时间相同时成本最低的建设方案。采用SPFA算法进行最短路径计算,并考虑了当到达某城市的最短时间相同时选择花费最小的情况。

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



















先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值