Questions about skiing and time capsules bzoj2753

Source: Internet
Author: User

I really can't stand it ~~~

A good noip "Touch your game" results in a question selected by Sichuan Province ....... Then, there will be no more

Don't talk about it. Go to the question!

[Scoi2012] skiing and time capsules

 

Description

 

A180285 enjoys skiing. He came to a snow-capped mountains, where M slide tracks are distributed and the intersection of N tracks (also scenic spots ), and each Scenic Spot has an I (1 <= I & lt; = N) and a height of HI. A180285 slides from scenic spots I to scenic spots J. If there is only one edge between I and j, and the height of I is not less than J. Unlike other skiing enthusiasts, a180285 prefers to use the shortest slide path to access as many scenic spots as possible. If you only visit the scenic spots on one path, he will feel that the number is too small. So a180285 took out the time capsule he carried with him. This is a magical drug. After eating it, you can immediately return to the previous scenic spot (without moving or being considered as a taxi distance of a180285 ). Please note that this magical drug can be continuously consumed, that is, it can return to the scenic spots that have been visited for a long time (such as the scenic spots that have passed through and those that have passed through ). Now, a180285 stands at the No. 1 scenic spot and looks at the target under the hill. He was wondering not to consider the time.

When capsules are consumed, slide to as many scenic spots as possible based on the shortest sliding distance (that is, the shortest distance is minimized when the maximum number of scenic spots passes through ). Can you help him find the shortest distance and the number of scenic spots?

 

Input

 

The first line of the input is two integers, N and M.

The next line contains N integers hi, indicating the height of each scenic spot.

Next, the M line shows the track distribution between scenic spots. Each line has three integers, UI, VI, and Ki. Indicates

There is a track with the length of KI between the scenic spots numbered with UI and those numbered with VI.

 

Output

 

 

Output a line, indicating the maximum number of scenic spots that can be reached in a180285, and the sum of the shortest taxi distance at this time.

 

Sample Input


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

Sample output

3 2

Hint

[Data Scope]

For 30% of data, ensure 1 <= n <= 2000

For 100% of data, ensure 1 <= n <= 100000

For all data, ensure that 1 <= m <= 000000,1 <= Hi <= 000000000,1 <= Ki <= 1000000000.

 

Well, I don't know whether you can afford it or not.

The question of a minimum tree structure is displayed. The range of data with so many zeros following his yarn 1 is hard to tolerate even three seconds.

Therefore, we have to select a new method to solve the problem.

Conversion of ideas is a necessary idea. However ~~ If it can be converted, it depends on the individual's skill.

During the morning simulation competition, I had already thought of generating a tree to the smallest node, and played a heap on the left to optimize the prim water. On the 1st, the snow-capped mountains began to expand outward, the sample can be used, and then we hit a left-biased heap to optimize the prim (You may ask me why I use the left-biased heap (also called the left heap, left-side tree). I will tell you that it's not because of the speed, but because I won't hit the binary heap ......)

If you don't want to talk about it, Prim actually has a major bug in this question, because when we use prim to scale out, we won't update the previously recorded values like triangle inequalities, so in the directed graph, there will be situations where we can't get through, but we can get back without updating our previous values .......

It is not easy to understand, but it is indeed incorrect. You can give a counterexample.

3 3

3 2 1

1 2 7

2 3 1

1 3 5

In this case, Prim is used to draw the conclusion that 5 + 7 = 12. In fact, we can first go to and then from. In this way, the optimal solution is 7 + 1 = 8;

What is the correct solution?

The positive solution uses kruscal. Why, because after we sort the edges in a certain order, we can ensure that we can first go higher, then go lower, and then go bigger. In this way, the above update will not happen.

So we sort the edge by double keywords: We rank the top of the vertex height that the edge reaches. If the height is the same, the preferred side is smaller.

In this way, we can ensure that we first find a high altitude point, and then take advantage of the situation and go down to the low point for updates.

Another thing: Edge building

If the two points connected are at the same altitude, two-way construction is performed. High [s]> high [T] is created forward, and high [s]

Well, don't forget to open long. I have a hand left to open long while ans use int, so Wa went three times ....

AC code:

  1 #include<iostream>  2 #include<cstdio>  3 #include<algorithm>  4 #include<cstring>  5 #define INF 970232024  6 #include<queue>  7 using namespace std;  8 struct edge  9 { 10     int s,t,next; 11     long long w; 12 }ed[2000020]; 13 int tot=0; 14 int ans=0; 15 int head[1000020]; 16 int high[1000020]; 17 long long dis[1000020]; 18 int n,m; 19 void add(int s,int t,int w) 20 { 21     ed[tot].s=s; ed[tot].t=t; ed[tot].w=(long long)w; ed[tot].next=head[s]; head[s]=tot++; 22 } 23 bool cmp(edge a,edge b) 24 { 25     if(high[a.t]==high[b.t]) 26     { 27         return a.w<b.w; 28     } 29     else return high[a.t]>high[b.t]; 30 } 31 bool vis[1000020]; 32 int cnt; 33 int f[1000020]; 34 bool need[1000020]; 35 bool inq[1000010]; 36 int find(int x) 37 { 38     if(f[x]==x)return x; 39     return f[x]=find(f[x]); 40 } 41 void kruscal() 42 { 43     sort(ed,ed+tot-1,cmp); 44   //  for(int i=0;i<tot;i++)cout<<ed[i].s<<" "<<ed[i].t<<" "<<ed[i].w<<endl; 45     for(int i=0;i<tot;i++) 46     { 47         int x=ed[i].s; 48         int y=ed[i].t; 49         int f1=find(x); 50         int f2=find(y); 51         if(f1!=f2 && need[x] && need[y]) 52         { 53             ans+=ed[i].w; 54             f[f1]=f2; 55         } 56     } 57 } 58 int main() 59 { 60  //   freopen("ski.in","r",stdin); 61  //   freopen("ski.out","w",stdout); 62     memset(head,-1,sizeof head); 63     scanf("%d%d",&n,&m); 64     for(int i=1;i<=n;i++) 65     { 66         f[i]=i; 67         dis[n]=INF; 68         scanf("%d",&high[i]); 69     } 70     while(m--) 71     { 72         int a,b; 73         long long c; 74         scanf("%d%d%lld",&a,&b,&c); 75         if(high[a]>high[b])add(a,b,c); 76         else if(high[a]==high[b]) 77         { 78             add(a,b,c); 79             add(b,a,c); 80         } 81         else add(b,a,c); 82     } 83     queue<int> q; 84     q.push(1); 85     while(!q.empty()) 86     { 87         int x=q.front(); 88         q.pop();vis[x]=true; 89         for(int i=head[x];i!=-1;i=ed[i].next) 90         { 91             if(!vis[ed[i].t]) 92             { 93                 q.push(ed[i].t); 94             } 95         } 96     } 97     for(int i=1;i<=n;i++) 98     { 99         if(vis[i])100         {101             cnt++;102             need[i]=true;103         }104     }105     cout<<cnt<<" ";106     memset(vis,false,sizeof vis);107     kruscal();108     cout<<ans<<endl;109 }
View code

 

Questions about skiing and time capsules bzoj2753

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.