ZOJ 3946 Highway Project(最短路)

本文详细介绍了如何通过SPFA算法解决从单一节点出发到达其他所有节点的最小时间代价问题,并在此基础上进一步优化成本。通过实例分析,读者可以深入理解算法的具体应用和优化策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3946


题面:

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

Author:  Lu, Yi
Source:  The 13th Zhejiang Provincial Collegiate Programming Contest


题目大意:

    求从0点出发单独到其他点的最小时间代价,并求得加的边代价最小。


解题:

    spfa加边,若当前点加入点使代价发生变化,且该点不在队列中,则将该点加入队列,并更新最小值。倘若该点时间代价与原有值相同,则比较到该点对应边的代价,若新代价小,则更新新的代价。若时间代价比原有大,则不更新。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define inf 0x3f3f3f3f
#define LL long long 
using namespace std;
int head[100010],cnt,next[200010];
LL cost[100010],dist[100010];
bool vis[100010];
struct edge
{
	int fm,to;
	LL ti,cost;
}E[200010];
void addedge(int u,int v,LL ti,LL cos)
{
  next[cnt]=head[u];
  head[u]=cnt;
  E[cnt].fm=u;
  E[cnt].to=v;
  E[cnt].ti=ti;
  E[cnt].cost=cos;
  cnt++;
}
queue <int> q;
int main()
{
	int n,m,t,fm,to,d,c,tmp;
	LL timecost,moneycost,val;
	scanf("%d",&t);
    while(t--)
	{
		timecost=moneycost=0;
		while(!q.empty())
			q.pop();
		cnt=0;
		memset(head,-1,sizeof(head));
		memset(next,-1,sizeof(next));
		memset(cost,inf,sizeof(inf));
		memset(vis,0,sizeof(vis));
		memset(dist,inf,sizeof(dist));
		scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
		{
			scanf("%d%d%lld%lld",&fm,&to,&d,&c);
			addedge(fm,to,d,c);
			addedge(to,fm,d,c);
		}
        q.push(0);
		vis[0]=1;
		dist[0]=0;
		while(!q.empty())
		{
           tmp=q.front();
		   q.pop();
		   vis[tmp]=0;
		   for(int i=head[tmp];~i;i=next[i])
		   {
			   val=dist[E[i].fm]+E[i].ti;
			   if(val<dist[E[i].to])
			   {
				   dist[E[i].to]=val;
				   if(!vis[E[i].to])
				   {
					   q.push(E[i].to);
					   vis[E[i].to]=1;
				   }
				   cost[E[i].to]=E[i].cost;
			   }
			   else if(val==dist[E[i].to])
			   {
				   if(cost[E[i].to]>E[i].cost)
					   cost[E[i].to]=E[i].cost;
			   }
			   else
				   continue;
		   }
		}
		for(int i=1;i<n;i++)
		{
			moneycost+=cost[i];
			timecost+=dist[i];
		}
		printf("%lld %lld\n",timecost,moneycost);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值