[STL-map]ACM训练赛第一场 E题 Message

这个题是我来西南交大以来第一场训练赛中做的题,在这个题中我学会了map的简单应用
题目

描述

M wants to send a message to his girlfriend N. Their language consists of n words numbered from 1 to n. Some words have the same meaning so there are k groups of words such that all the words in some group have the same meaning.

M knows that the i-th word can be sent with cost ai. For each word in his message, M can either replace it with another word of the same meaning or leave it as it is. Can you help M determine the minimum cost of sending the message?

The cost of sending the message is the sum of the costs of sending every word in it.

输入

The first line of input contains integers n, k and m (1≤ k ≤ n ≤ 1e5, 1 ≤ m ≤ 1e5) — the number of words in their language, the number of groups of words, and the number of words in M’s message respectively.

The second line contains n strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It’s guaranteed that the words are distinct.

The third line contains n integers a1, a2, …, an (1 ≤ ai ≤ 1e9) where ai is the cost of sending the i-th word.

The next k lines describe the groups of words of same meaning. The next k lines each start with an integer x (1 ≤ x ≤ n) which means that there are x words in this group, followed by x integers which represent the indices of words in this group. It’s guaranteed that each word appears in exactly one group.

The next line contains m space-separated words which represent M’s message. Each of these words appears in the list of language’s words.

输出

The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning.

输入样例1

5 4 4
i loser am the second
100 1 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

输出样例1

107

输入样例2

5 4 4
i not am the second
100 20 1 5 10
1 1
1 3
2 2 5
1 4
i am the second

输出样例2

116

提示

In the first sample, M should replace the word “second” with the word “loser” because it has less cost so the cost will be 100+1+5+1=107.

In the second sample, M shouldn’t do any replacement so the cost will be 100+1+5+10=116.

题解

这个题就是map的典型应用
先简单介绍一下map的用法
map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一 种非严格意义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的,后边我们会见识到有序的好处。引用来自网上
clear
清除 map 中所有元素;
erase
删除 map 中指定位置的元素;
insert
在 map 指定位置添加 pair 类型的元素;
find
获取 map 中元素的迭代器;
begin, end
map 的正向迭代器的起始位置与终点位置;
rbegin, rend

map可以通过迭代器进行遍历


这个题要求我们求出 发送信息的最少花费,不同的单词可能意思相同,花费一样

我们可以记录同一组的花费是多少,然后用map记录单词属于哪一组,这样可以轻易得到最小花费

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn = 1e5 + 10;
int cost[maxn];
int mc[maxn];

int main(){
	memset(mc, 0x7f, sizeof(mc));
	int n, m, k;
	long long ans = 0;
	cin >> n >> k >> m;
	string a[maxn], b;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n; i++) cin >> cost[i];
	map<string, int> s;
	for(int i = 1; i <= k; i++){
		int d;
		scanf("%d", &d);
		while(d--){
			int pos;
			scanf("%d", &pos);
			s.insert(make_pair(a[pos], i));
			mc[i] = min(mc[i], cost[pos]);
		}
	}
	map<string, int>::iterator iter;// 刻意指向map寻找的位置
	for(int i = 1; i <= m; i++){
		cin >> b;
		iter = s.find(b);
		ans += mc[iter->second];
	}
	cout << ans;
	return 0;
}

就是这样 map的做法很简单

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值