Travel Cards CodeForces - 847K

In the evening Polycarp decided to analyze his today's travel expenses on public transport.

The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops.

Polycarp made n trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes.

It is known that one trip on any bus costs a burles. In case when passenger makes a transshipment the cost of trip decreases to b burles (b < a). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment.

For example, if Polycarp made three consecutive trips: "BerBank" "University", "University" "BerMall", "University" "BerBank", then he payed a + b + a = 2a + b burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus.

Also Polycarp can buy no more than k travel cards. Each travel card costs f burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction.

What is the smallest amount of money Polycarp could have spent today if he can buy no more than k travel cards?

Input

The first line contains five integers n, a, b, k, f (1 ≤ n ≤ 300, 1 ≤ b < a ≤ 100, 0 ≤ k ≤ 300, 1 ≤ f ≤ 1000) where:

  • n — the number of Polycarp trips,
  • a — the cost of a regualar single trip,
  • b — the cost of a trip after a transshipment,
  • k — the maximum number of travel cards Polycarp can buy,
  • f — the cost of a single travel card.

The following n lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space — the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different.

Output

Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than k travel cards.

Example

Input
3 5 3 1 8
BerBank University
University BerMall
University BerBank
Output
11
Input
4 2 1 300 1000
a A
A aa
aa AA
AA a
Output
5

Note

In the first example Polycarp can buy travel card for the route "BerBank University" and spend 8 burles. Note that his second trip "University" "BerMall" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles.

In the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2 + 1 + 1 + 1 = 5 burles.

题意:Polycarp有张旅行表,上面有n次旅行,不换乘的话,每次车票a元,否则每次车票b元。除此之外,他能购买不超过k张神奇

的车票(通票):n次旅行中起点和终点是相同(无对应关系,如①(1 2 )②(2 3)③(2 1),①和③可以是通票)的车站,

去和回都不收钱。问n次最少花费多少钱?

题解:贪心,把两站之间的花费(经过次数乘以车票价格)较多的用通票换掉就行了。熟悉了map的用法

 1 #include<bits/stdc++.h>
 2 #define P(xx,yy) make_pair(min(xx,yy),max(xx,yy))      //技巧,省略了比较
 3 using namespace std;
 4 
 5 int q[400];
 6 int n,x,y,k,f;
 7 
 8 bool cmp(int aa,int bb){
 9     return aa>bb;
10 } 
11 
12 int main()
13 {   cin>>n>>x>>y>>k>>f;
14     map<string,int> pp;
15     map<pair<int,int>,int> p;
16     
17     string a,b;
18     int ans=0,coo=0,cnt=0,temp=-1;
19     for(int i=1;i<=n;i++){
20         cin>>a>>b;
21         if(!pp[a]) pp[a]=++cnt;          //对车站编号
22         if(!pp[b]) pp[b]=++cnt;
23         coo=(pp[a]==temp?y:x);           //判断是否转站(第i-1次的尾是否是第i次的头)
24         p[P(pp[a],pp[b])]+=coo;          //计算a站和b站之间用的总花费:次数*车票价格
25         
26         //cout<<pp[a]<<" "<<pp[b]<<endl;
27         ans=ans+coo;                     //不用通票的情况下总的花费
28         temp=pp[b];
29     }
30         
31     cnt=0;
32     for(auto i=p.begin();i!=p.end();i++) q[cnt++]=i->second;
33 
34     sort(q,q+cnt,cmp);
35     for(int i=0;i<k;i++){
36         if(q[i]<f) break;
37         else ans-=q[i]-f;                //把花费大的用通票换掉     
38     }
39     cout<<ans<<endl;
40 }

 

posted @ 2017-10-25 23:31  天之道,利而不害  阅读(493)  评论(2编辑  收藏  举报