Ski Valley P2573 minimum spanning tree

Posted by SheDesigns on Sat, 08 Jan 2022 07:58:45 +0100

Title Description

a180285 likes skiing very much. He came to a snow mountain, where there are , mm , taxiing tracks and , nn , track intersections (also scenic spots), and each scenic spot has a number , i\space (1 \le i \le n)i (1 ≤ i ≤ n) and a height , h_ihi​.

A180285 can slide from the scenic spot ^ ii ^ to the scenic spot ^ jj ^ if and only if there is an edge between ^ ii ^ and ^ jj ^ and the height of ^ ii ^ is not less than. Unlike other skiing enthusiasts, a180285 likes to use the shortest taxiing path to visit as many scenic spots as possible. If he only visits the scenic spots on one path, he will feel that the number is too small.

So a18028 5 took out the time capsule he carried with him. This is a magical medicine. After eating it, you can immediately return to the last scenic spot (without moving or being considered as the sliding distance of a180285).

Please note that this magical medicine can be eaten continuously, that is, it can return to the scenic spots that we visited a long time ago (such as the last scenic spot and the last scenic spot). Now, a180285 stands in scenic spot No. 11 , looking at the target at the foot of the mountain. He is eager to know the scheme of sliding to as many scenic spots as possible with the shortest sliding distance without considering the consumption of time capsule (i.e. minimizing the total sliding distance on the premise of meeting the maximum number of scenic spots). Can you help him find the shortest distance and the number of scenic spots?

Input format

The first line of input is two integers n,mn,m. The next line has # nn # integers # h_ihi, indicating the height of each scenic spot respectively.

Next, the , mm , line represents the track distribution among various scenic spots. Three integers , u,v,ku,v,k in each line indicate that there is a track with a length of , kk , between the scenic spot numbered , uu , and the scenic spot numbered , vv ,.

Output format

Output a line indicating the maximum number of scenic spots that a180285 can reach and the sum of the shortest taxiing distance at this time.

Input and output samples

Enter #1 copy

3 3 
3 2 1 
1 2 1 
2 3 1 
1 3 10 

Output #1 copy

3 2

Description / tips

[data range]
For ﹤ 30 \% 30% data, 1 \le n \le 20001 ≤ n ≤ 2000;
For 100 \% 100% data, 1 \le n \le 10^51 ≤ n ≤ 105.

For all data, ensure 1 \le m \le 10^61 ≤ m ≤ 106, 1 \le h_i \le 10^91≤hi​≤109 ,1 \le k_i \le 10^91≤ki​≤109.

Heap optimized prim

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100010;const int MAX = 0x3f3f3f3f;
int height[MAXN],cnt,t,n,pre[MAXN],weight[MAXN],vis[MAXN];
struct info{
    int to,w,nt;
struct qnode{
    int v,h,dis;
    qnode(int _v = 0,int _h = 0,int _dis = 0) : v(_v),h(_h),dis(_dis){}
    bool operator < (const qnode & s) const{
        return h == s.h ? dis > s.dis : h < s.h;
void add(int x,int y,int z)
	node[cnt].to = y;
	node[cnt].w = z;
	node[cnt].nt = pre[x];
	pre[x] = cnt;
ll prim()
    ll sum = 0;cnt = 0;
    priority_queue<qnode> q;
    weight[1] = 0;
        qnode temp =;q.pop();
        int u = temp.v;
        if(vis[u]) continue;
        cnt++;sum += weight[u];
        vis[u] = 1;
        for(int i = pre[u]; i ;i = node[i].nt){
            if(!vis[node[i].to] && node[i].w < weight[node[i].to]){
                weight[node[i].to] = node[i].w;
    return sum;
int main()
    int m,t1,t2,w;
    scanf("%d %d",&n,&m);
    for(int i = 1;i <= n; i++)
    for(int i = 0;i < m; i++){
        scanf("%d %d %d",&t1,&t2,&w);
        if(height[t1] >= height[t2])
        if(height[t2] >= height[t1])
    ll res = prim(); 
    printf("%d %lld",cnt,res);
    return 0;

Topics: C++ Algorithm Graph Theory