Codeforces Round # 636 E. Weights Distributing (shortest path)

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.

Input

The first line of the input contains one integer tt (1t1041≤t≤104 ) — 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 (2n21052≤n≤2⋅105 , n1mmin(n(n1)2,2105)n−1≤m≤min(n(n−1)2,2⋅105) , 1a,b,cn1≤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 p1,p2,,pmp1,p2,…,pm (1pi1091≤pi≤109 ), where pipi 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 vivi , uiui (1vi,uin1≤vi,ui≤n , uiviui≠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,uivi,ui ) there are no other pairs (vi,uivi,ui ) or (ui,viui,vi ) in the array of edges, and for each pair (vi,ui)(vi,ui) the condition viuivi≠ui 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 21052⋅105 (n2105∑n≤2⋅105 , m2105∑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
Copy
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
Copy
7 
12 
At the beginning, I thought about finding the shortest path from a to b and the shortest path from b to c. Counting which paths are repeated, and give priority to small weights. But this is wrong. Directly copy a paragraph in the CF question comment: "You cannot do the question on basis of only considering shortest from A to B and then shortest from B to C separately. Consider smallest path from A to B (length s) and there is another path from A to B (length s + 1) which has only one more edge then the smallest one. Suppose that path contains C, so that is more advantageous and we need only smallest s + 1 prices while with the shortest path we will need then s + k prices (k distance of B from C). "The
positive solution is to find the prefix sum after sorting the edge weights. Starting from the three points a, b, and c, find the shortest path length from these three points to the remaining points, and then enumerate each point x (can be abc itself), you can find that the path is composed of four parts: a- > x x-> b b-> x x-> c where x-> b is equivalent to walking twice, so priority is given to the edge of the x-> b path with a small weight, then a-> x and x- > c. You can use the prefix sum to update the cumulative answer.
You may think of what to do if you repeatedly walk along certain edges, such as the chain of xabc. In fact, this has no effect, because this situation must not be the optimal solution. Observation shows that the optimal solution can only be similar to that in the example.
Note that it is necessary to judge when updating the answer at the end. The path length exceeds m and is skipped.
#include <bits/stdc++.h>
#define N 200005
#define M 200005
using namespace std;
int n,m,a,b,c,head[N],ver[2*M],Next[2*M],dist[3][N],tot=0;
long long edge[M];
void add(int x,int y)
{
    ver[++tot]=y,Next[tot]=head[x],head[x]=tot;
}
struct node
{
    int num;
    int cnt;
};
void bfs(int num,int x)
{
    bool vis[200005]={0};
    queue<node>q;
    node start={num,0};
    q.push(start);
    while(q.size())
    {
        node pre=q.front();
        q.pop();
        if(vis[pre.num])continue;
        vis[pre.num]=1;
        dist[x][pre.num]=pre.cnt;
        int i;
        for(i=head[pre.num];i;i=Next[i])
        {
            int y=ver[i];
            if(vis[y]) continue;
            node nxt;
            nxt.cnt=pre.cnt+1;
            nxt.num=y;
            q.push(nxt);
        }
    }
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        tot=0;
        scanf("%d%d%d%d%d",&n,&m,&a,&b,&c);
        long long i;
        for(i=1;i<=max((long long)n,min((long long)200002,(long long)n*(n-1)/2));i++)
        {
            dist[0][i]=dist[1][i]=dist[2][i]=head[i]=ver[i]=Next[i]=edge[i]=0;
        }
        for(i=1;i<=m;i++)scanf("%lld",&edge[i]);
        sort(edge+1,edge+m+1);
        edge[0]=0;
        for(i=1;i<=m;i++)edge[i]+=edge[i-1];//求前缀和方便后续 
        for(i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        bfs(a,0);
        bfs(b,1);
        bfs(c,2);
        long long ans=~0ull>>2;
        for(i=1;i<=n;i++)//枚举点 
        {
            if(dist[0][i]+dist[1][i]+dist[2][i]>m)continue;
            long long temp=edge[dist[0][i]+dist[1][i]+dist[2][i]]+edge[dist[1][i]];
            ans=min(ans,temp);
        }
        cout<<ans<<endl;
    }    
    return 0;    
} 

 

Guess you like

Origin www.cnblogs.com/lipoicyclic/p/12761501.html