Problem D

Lost in Translation

The word is out that you’ve just finished writing a book entitled How to Ensure Victory at a Programming Contest and requests are flying in. Not surprisingly, many of these requests are from foreign countries, and while you are versed in many programming languages, most spoken languages are Greek to you. You’ve done some investigating and have found several people who can translate between languages, but at various costs. In some cases multiple translations might be needed. For example, if you can’t find a person who can translate your book from English to Swedish, but have one person who can translate from English to French and another from French to Swedish, then you’re set. While minimizing the total cost of all these translations is important to you, the most important condition is to minimize each target language’s distance (in translations) from English, since this cuts down on the errors that typically crop up during any translation. Fortunately, the method to solve this problem is in Chapter 7 of your new book, so you should have no problem in solving this, right?

Input

Input starts with a line containing two integers n m indicating the number of target languages and the number of translators at your disposal (1 ≤ n ≤ 100, 1 ≤ m ≤ 4500). The second line will contain n strings specifying the n target languages. After this line are m lines of the form l1 l2 c where l1 and l2 are two different languages and c is a positive integer specifying the cost to translate between them (in either direction). The languages l1 and l2 are always either English or one of the target languages, and any pair of languages will appear at most once in the input. The initial book is always written in English.

Output

Display the minimum cost to translate your book to all of the target languages, subject to the constraints described above, or Impossible if it is not possible.

Sample Input 1

4 6

Pashto French Amheric Swedish

English Pashto 1 English French 1

English Amheric 5

Pashto Amheric 1

Amheric Swedish 5

French Swedish 1

Sample Output 1

8

Sample Input 2

2 1

A B

English B 1

Sample Output 2

Impossible

ECNA 2016 Problem D: Lost in Translation

题意:

以Englis为源点,并再给出n个点和m条边,并给出这n个点的名字,m条边中会给出两个邻接点和边的权值(边是无向边),问从源点开始是否能到达所有边,如果能到达输出离源点最近路径中的最小代价之和,否则输出Impossible

思路:

此题的坑点在于最小代价之和是需要在离源点最近路径中找,也就是说若设源点的深度为0,用bfs搜索,每一个节点的深度为第一个与他相连的节点的深度+1,则在计算每个点的最小代价时只能从深度和他相差1的节点更新,这样才能保证离源点最近
否则你就会一直卡在test9,还有注意判断相等要用==,一开始我想到了记录深度的问题,结果还是WA到自闭,结果才发现判断相等的==写成了=,以后注意要多检查if,while的判断条件之类的

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll amn=,inf=1e18;
struct node{
ll i,w;
node(ll ii,ll ww){i=ii,w=ww;}
};
vector<node> eg[amn];
queue<int> q;
string nu,nv;
map<string,int> mp;
ll n,m,cost[amn],deep[amn];
void bfs(int s){
deep[s]=; ///源点深度初始化为0
while(q.size())q.pop();
q.push(s);
while(q.size()){
int u=q.front();q.pop();
for(int i=;i<eg[u].size();i++){
ll v=eg[u][i].i,w=eg[u][i].w;
if(deep[v]==inf)deep[v]=deep[u]+; ///如果是第一次访问就把当前节点的深度设为父节点的深度+1
if(deep[v]==deep[u]+){ ///当前节点为父节点的深度+1时才能更新代价最小值并加入搜索队列
cost[v]=min(cost[v],w);
q.push(v);
}
}
}
}
int main(){
ios::sync_with_stdio();
cin>>n>>m;
mp["English"]=; ///English作为源点编号为0
for(int i=;i<=n;i++){
deep[i]=cost[i]=inf;
cin>>nu;
mp[nu]=i; ///给每种语言作为节点编号
}
int w;
for(int i=;i<m;i++){
cin>>nu>>nv>>w;
node u(mp[nu],w),v(mp[nv],w);
eg[u.i].push_back(v);
eg[v.i].push_back(u);
}
bfs(); ///从源点开始bfs
bool valid=;
ll ans=;
for(int i=;i<=n;i++){
if(cost[i]==inf){valid=;break;} ///如果碰到代价为inf的点,也就是说明源点无法到这个点,则说明情况非法,要输出Impossible
ans+=cost[i]; ///记录最小代价之和
}
if(valid)
printf("%lld\n",ans);
else
printf("Impossible\n");
}
/**
以Englis为源点,并再给出n个点和m条边,并给出这n个点的名字,m条边中会给出两个邻接点和边的权值(边是无向边),问从源点开始是否能到达所有边,如果能到达输出离源点最近路径中的最小代价之和,否则输出Impossible
此题的坑点在于最小代价之和是需要在离源点最近路径中找,也就是说若设源点的深度为0,每一个节点的深度为第一个与他相连的节点的深度+1,则在计算每个点的最小代价时只能从深度和他相差1的节点更新,这样才能保证离源点最近
否则你就会一直卡在test9,还有注意判断相等要用==,一开始我想到了记录深度的问题,结果还是WA到自闭,结果才发现判断相等的==写成了=
**/

[bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation的更多相关文章

  1. 2017-2018 ACM-ICPC East Central North America Regional Contest (ECNA 2017) Solution

    A:Abstract Art 题意:给出n个多边形,求n个多边形分别的面积和,以及面积并 思路:模板 #include <bits/stdc++.h> using namespace st ...

  2. Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)

    A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...

  3. 2016-2017 ACM-ICPC East Central North America Regional Contest (ECNA 2016) F 区间dp

    Problem F Removal GameBobby Roberts is totally bored in his algorithms class, so he’s developed a li ...

  4. 2014-2015 ACM-ICPC East Central North America Regional Contest (ECNA 2014) A、Continued Fractions 【模拟连分数】

    任意门:http://codeforces.com/gym/100641/attachments Con + tin/(ued + Frac/tions) Time Limit: 3000/1000 ...

  5. MPI Maelstrom(East Central North America 1996)(poj1502)

    MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercom ...

  6. poj 2732 Countdown(East Central North America 2005)

    题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...

  7. East Central North America Region 2015

    E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...

  8. POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)

    题目链接 问题描述 : We are all familiar with pre-order, in-order and post-order traversals of binary trees. ...

  9. POJ 1240 Pre-Post-erous! && East Central North America 2002 (由前序后序遍历序列推出M叉树的种类)

    题目链接:http://poj.org/problem?id=1240 本文链接:http://www.cnblogs.com/Ash-ly/p/5482520.html 题意: 通过一棵二叉树的中序 ...

随机推荐

  1. 分析Java中的length和length()

    在不适用任何带有自动补全功能的IDE的情况下,我们怎么获取一个数组的长度?如何获取字符串的长度? 这里我们先举用实例去分析一下:int[] arr=new int[3]:System.out.prin ...

  2. 某某项目SDV软件测试报告范例

    说明:本范例为符合CMMI 5级要求的范例 Prepared by 拟制 小张 Date 日期 2008-04-09 Reviewed by 评审人 小丽.小王.小李.小莉.小三.小四.小猪.小猫.小 ...

  3. 没有图片的freemarker下载,备份

    没有图片的freemarker下载,备份 //以下代码也可以使用/* public String exportApproveCase(@PathVariable("proId") ...

  4. 【DirectX 11学习笔记】世界矩阵的理解-运动合成

    最近在看龙书,写一下自己的学习理解,主要是物体运动的合成. 物体于局部坐标系内构建,每个物体拥有自己的局部坐标系以及相应的顶点矩阵A,并通过世界矩阵变换到唯一的世界坐标系. 物体在某时刻发生了位移和旋 ...

  5. C#爬取微博文字、图片、视频(不使用Cookie)

    前两天在网上偶然看到一个大佬OmegaXYZ写的文章,Python爬取微博文字与图片(不使用Cookie) 于是就心血来潮,顺手撸一个C#版本的. 其实原理也很简单,现在网上大多数版本都需要Cooki ...

  6. 【推荐算法工程师技术栈系列】分布式&数据库--tensorflow

    目录 TensorFlow 高阶API Dataset(tf.data) Estimator(tf.estimator) FeatureColumns(tf.feature_column) tf.nn ...

  7. win10查看本机mac地址的详细操作

    今天和大家分享win10查看本机mac地址的方法,mac地址是什么东西?MAC地址实际上就是网卡的一个标识,和身份证号码类似,大多数情况下是不需要关心MAC地址是多少的,一般不能改动,所以也不会重复. ...

  8. JavaScript的数组系列

    数组 今天逆战班的学习主题关于Javascript的数组,主要有数组的概念.创建.分类.方法.遍历.经典算法...... 一.数组是什么呢?怎么写数组呢?数组有多少种呢? 数组的概念 对象是属性的无序 ...

  9. C# 存储相同键多个值的Dictionary

    涉及到两个问题: 一.访问磁盘中文件夹.文件夹下面的文件夹 先看一下磁盘文件夹结构 C盘下面有个根文件夹SaveFile,SaveFIle下面有两个子文件夹分别为,2018.2019, 子文件下201 ...

  10. linux 安装Mosquitto

    这篇博客讲的很好:https://www.cnblogs.com/chen1-kerr/p/7258487.html 此处简单摘抄部分内容 1.下载mosquitto安装包 源码地址:http://m ...