Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- It's easy to see that for every word, the minimum cost of sending it is the minimum cost of sending any word in its group. For each group, we'll maintain the minimum cost for sending a word in it (let it be costi) and for each word, we'll maintain its group (let it be groupi). For every word i in the message, we'll add costgroupi to the answer.
- Time complexity O((n + m)log(n) * len).
- #include <iostream>
- #include <map>
- using namespace std;
- map<string,int> d;
- int arr[100005],cost[100005],group[100005];
- int main()
- {
- int n,k,m;
- cin >> n >> k >> m;
- for (int i=1;i<=n;i++)
- {
- string s;
- cin >> s;
- d[s]=i;
- }
- for (int i=1;i<=n;i++)
- {
- cin >> arr[i];
- cost[i]=(1<<30);
- }
- for (int i=1;i<=k;i++)
- {
- int x;
- cin >> x;
- while (x--)
- {
- int a;
- cin >> a;
- group[a]=i;
- cost[i]=min(cost[i],arr[a]);
- }
- }
- long long ans=0;
- while (m--)
- {
- string s;
- cin >> s;
- ans+=cost[group[d[s]]];
- }
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment