Weights Distributing CodeForces - 1343E(思维+最短路)

You are given an undirected unweighted graph consisting of n vertices and m edges (which represents the map of Bertown) and the array of prices p of length m. It is guaranteed that there is a path between each pair of vertices (districts).

Mike has planned a trip from the vertex (district) a to the vertex (district) b and then from the vertex (district) b to the vertex (district) c. 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 p 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 t independent test cases.

Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of the test case contains five integers n,m,a,b and c (2≤n≤2⋅105, n−1≤m≤min(n(n−1)2,2⋅105), 1≤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 m integers p1,p2,…,pm (1≤pi≤109), where pi is the i-th price from the array.

The following m lines of the test case denote edges: edge i is represented by a pair of integers vi, ui (1≤vi,ui≤n, ui≠vi), 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 (vi,ui) there are no other pairs (vi,ui) or (ui,vi) in the array of edges, and for each pair (vi,ui) the condition vi≠ui is satisfied. It is guaranteed that the given graph is connected.

It is guaranteed that the sum of n (as well as the sum of m) does not exceed 2⋅105 (∑n≤2⋅105, ∑m≤2⋅105).

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

Example
Input
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
Output
7
12
Note
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-c的最短路无非就两种情况。
①a-b-c,中间不经过别的转折点。
②a-x-b-x-c,中间经过别的转折点。
这样的话,如果x==b的话,那么第二种情况也就变成第一种情况了。因此第一种情况是第二种情况的一种特殊形式。
这样的话,我们就需要求出来a-x,b-x,c-x的最短路径。三次spfa。求出来之后,我们就枚举x,不断的更新最小值,有点细节需要注意,见代码
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
struct edge{
	int to,next;
}e[maxx<<1];
int head[maxx<<1];
int disa[maxx],disb[maxx],disc[maxx],dis[maxx],vis[maxx];
ll sum[maxx];
int n,m,a,b,c,tot;

inline void init()
{
	tot=0;
	for(int i=0;i<=2*m+10;i++) head[i]=-1;
	for(int i=0;i<=n+1;i++) disa[i]=disb[i]=disc[i]=inf;
}
inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
inline void spfa(int d[],int s)
{
	queue<int> q;
	for(int i=1;i<=n;i++) vis[i]=0;
	d[s]=0;
	vis[s]=1;
	q.push(s);
	while(q.size())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			int to=e[i].to;
			if(d[to]>d[u]+1)
			{
				d[to]=d[u]+1;
				if(vis[to]==0)
				{
					vis[to]=1;
					q.push(to);
				}
			}
		}
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d%d",&n,&m,&a,&b,&c);
		init();
		int x,y;
		for(int i=1;i<=m;i++) scanf("%d",&dis[i]);
		sort(dis+1,dis+1+m);
		sum[0]=0;
		for(int i=1;i<=m;i++) sum[i]=sum[i-1]+(ll)dis[i];
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			add(x,y);
			add(y,x);
		}
		spfa(disa,a);
		spfa(disb,b);
		spfa(disc,c);
		ll ans=1e18;
		ll sum1,sum2,sum3;
		for(int i=1;i<=n;i++)
		{
			if(disa[i]+disb[i]+disc[i]>m) continue;//有可能会超过边数,假如a=2,b=1,c=2,然后就只有一条1-2的边。当x=1的时候,三个的和就大于m,这样是不符合的,就直接跳过就可以了。
			ans=min(ans,sum[disa[i]+disb[i]+disc[i]]+sum[disb[i]]);//disb[i]条路是重复的,这几个重复的就添加上最小的边。
		}
		printf("%lld\n",ans);
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值