【CF1343E】 Weights Distributing

题目

题目描述
You are given an undirected unweighted graph consisting of nn vertices and mm edges (which represents the map of Bertown) and the array of prices pp of length mm . It is guaranteed that there is a path between each pair of vertices (districts).

Mike has planned a trip from the vertex (district) aa to the vertex (district) bb and then from the vertex (district) bb to the vertex (district) cc . He can visit the same district twice or more. But there is one issue: authorities of the city want to set a price for using the road so if someone goes along the road then he should pay the price corresponding to this road (he pays each time he goes along the road). The list of prices that will be used pp is ready and they just want to distribute it between all roads in the town in such a way that each price from the array corresponds to exactly one road.

You are a good friend of Mike (and suddenly a mayor of Bertown) and want to help him to make his trip as cheap as possible. So, your task is to distribute prices between roads in such a way that if Mike chooses the optimal path then the price of the trip is the minimum possible. Note that you cannot rearrange prices after the start of the trip.

You have to answer tt independent test cases.

输入格式
The first line of the input contains one integer tt ( 1 \le t \le 10^41≤t≤10
4
) — the number of test cases. Then tt test cases follow.

The first line of the test case contains five integers n, m, a, bn,m,a,b and cc ( 2 \le n \le 2 \cdot 10^52≤n≤2⋅10
5
, n-1 \le m \le min(\frac{n(n-1)}{2}, 2 \cdot 10^5)n−1≤m≤min(
2
n(n−1)

,2⋅10
5
) , 1 \le a, b, c \le n1≤a,b,c≤n ) — the number of vertices, the number of edges and districts in Mike’s trip.

The second line of the test case contains mm integers p_1, p_2, \dots, p_mp
1

,p
2

,…,p
m

( 1 \le p_i \le 10^91≤p
i

≤10
9
), where p_ip
i

is the ii -th price from the array.

The following mm lines of the test case denote edges: edge ii is represented by a pair of integers v_iv
i

, u_iu
i

( 1 \le v_i, u_i \le n1≤v
i

,u
i

≤n , u_i \ne v_iu
i



=v
i

), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i. e. for each pair ( v_i, u_iv
i

,u
i

) there are no other pairs ( v_i, u_iv
i

,u
i

) or ( u_i, v_iu
i

,v
i

) in the array of edges, and for each pair (v_i, u_i)(v
i

,u
i

) the condition v_i \ne u_iv
i



=u
i

is satisfied. It is guaranteed that the given graph is connected.

It is guaranteed that the sum of nn (as well as the sum of mm ) does not exceed 2 \cdot 10^52⋅10
5
( \sum n \le 2 \cdot 10^5∑n≤2⋅10
5
, \sum m \le 2 \cdot 10^5∑m≤2⋅10
5
).

输出格式
For each test case, print the answer — the minimum possible price of Mike’s trip if you distribute prices between edges optimally.

题意翻译
给出一个有 nn 个点,mm 条边的无向图和一个长为 mm 的权值序列 ww。

你可以随意安排边权(每条边权对应 ww 中的一个数,不可以重复)。

求 aa 到 bb 的最短路与 bb 到 cc 的最短路的和的最小值。

输入输出样例
输入 #1复制
2
4 3 2 3 4
1 2 3
1 2
1 3
1 4
7 9 1 5 7
2 10 4 8 5 6 7 3 3
1 2
1 3
1 4
3 2
3 5
4 2
5 6
1 7
6 7
输出 #1复制
7
12
说明/提示
One of the possible solution to the first test case of the example:

One of the possible solution to the second test case of the example:

思路

这题让我们对边权进行赋值,首先肯定想到,假如不考虑边权,那么我们也是要求a-b,b-c的路径最短是多少,因为路径少意味着权值少

这里有两种情况,一种是这两者之间存在某个点x,使得部分路径重复,一种是不存在

其实这两个是一种情况,第二种情况的b就是x。这样我们发现重复的路径要走两边,肯定是将他赋值到最小,而其他的,就贪心的从剩下小的里面拿

因此现在不考虑权值,那么直接求三遍bfs就能求出三个点到其他各点最少经过的路径,接着枚举中间点x,就能求出答案

这里还要注意的是,因为我们是暴力枚举,所以可能存在三条路径加起来大于总边数的情况,这种情况直接continue,因为显然不会选。

代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define N 200100
struct edge{int next,to;}e[N<<1];
int head[N],w[N],sum[N],n,m,A,B,C,disA[N],disC[N],disB[N],dis[N],cnt=-1;
void add(int x,int y)
{
	e[++cnt].next = head[x];
	e[cnt].to = y;
	head[x] = cnt;
}
void solve(int s,int t)
{
	queue<int> q;
	memset(dis,0x3f,sizeof(dis));
	dis[s]=0;q.push(s);
	while(!q.empty())
	{
		int x = q.front();
		q.pop();
		for(int i = head[x];~i;i=e[i].next)
		{
			int y = e[i].to;
			if(dis[y]==0x3f3f3f3f3f3f3f3f)
			{
				dis[y] = dis[x]+1;
				q.push(y);
			}
		}
	}
	if(t == 1) 
	for(int i = 1;i <= n;i++)disA[i]=dis[i];
	if(t == 2) 
	for(int i = 1;i <= n;i++)disB[i]=dis[i];
	if(t == 3) 
	for(int i = 1;i <= n;i++)disC[i]=dis[i];
}
signed main()
{
	int T=read();
	while(T--)
	{
		memset(head,-1,sizeof(head));
		memset(w,0,sizeof(w));
		cnt=-1;
		n=read();m=read();A=read();B=read();C=read();
		for(int i = 1;i <= m;i++)
		{
			w[i]=read();
		}
		sort(w+1,w+m+1);
		for(int i = 1;i <= m;i++) sum[i]=sum[i-1]+w[i];
		for(int i = 1;i <= m;i++)
		{
			int a=read(),b=read();
			add(a,b);
			add(b,a);
		}
		solve(A,1);
		solve(B,2);
		solve(C,3);
		int ans=0x3f3f3f3f3f3f3f3f;
		for(int i = 1;i <= n;i++)
		{
			int a=disA[i],b=disB[i],c=disC[i];
			if(a+b+c<=m)
			{
				ans=min(sum[a+b+c]+sum[b],ans);
			}
			
		}
		printf("%lld\n",ans);
		for(int i = 1;i <= n;i++)
		disA[i]=disB[i]=disC[i]=0x3f3f3f3f3f3f3f3f;
	}
	return 0;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值