[bfs,深度记录] East Central North America Regional Contest 2016 (ECNA 2016) D Lost in Translation
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的更多相关文章
- 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 ...
- Gym-101673 :East Central North America Regional Contest (ECNA 2017)(寒假自训第8场)
A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef lo ...
- 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 ...
- 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 ...
- MPI Maelstrom(East Central North America 1996)(poj1502)
MPI Maelstrom 总时间限制: 1000ms 内存限制: 65536kB 描述 BIT has recently taken delivery of their new supercom ...
- poj 2732 Countdown(East Central North America 2005)
题意:建一个家庭树,找出有第d代子孙的名字,按照要求的第d代子孙的数从大到小输出三个人名,如果有一样大小子孙数的,就按字母序从小到大将同等大小的都输出,如果小于三个人的就全输出. 题目链接:http: ...
- East Central North America Region 2015
E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 #include <iostream> #include <cstdio> #include <algo ...
- 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. ...
- 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 题意: 通过一棵二叉树的中序 ...
随机推荐
- 「知乎」对中国用户而言,Pure Android 是否比 MIUI 或 Flyme 体验更好? - Donnie的博客
这篇文章转载自我在知乎上的回答 哎呀-不要站队嘛.其实这是一个很有意思的题目,让我们一点点来看 哦对,谢妖-.本人是Nexus 5用户,系统当然是Pure Android KitKat啦(臭谷粉!点D ...
- RPi.GPIO 和 HM
后续笔记不再记录导入的模块和硬件的连接方法,请根据关键词自行搜索. RPi.GPIO模块 GPIO:General Purpose Input Output 即 通用输入/输出 RPi.GPIO是一个 ...
- 沈向洋|微软携手 OpenAI 进一步履行普及且全民化人工智能的使命
OpenAI 进一步履行普及且全民化人工智能的使命"> 作者简介 沈向洋,微软全球执行副总裁,微软人工智能及微软研究事业部负责人 我们正处于技术发展历程中的关键时刻. 云计算的强大计算 ...
- 【h5ai】搭建服务器目录
在前几天,我帮人安装h5ai这个东西,结果直接踩坑,装了一个下午,打算自己也装一个,顺便写一下教程 最终效果演示: https://larsjung.de/h5ai/demo/ 服务器 服务器这里推荐 ...
- Redis list实现原理 - 双向循环链表
双向链表 双向表示每个节点知道自己的直接前驱和直接后继,每个节点需要三个域 查找方向可以是从左往右也可以是从右往左,但是要实现从右往左还需要终端节点的地址,所以通常会设计成双向的循环链表; 双向的循环 ...
- 压力测试(六)-阿里云Linux服务器压测接口实战
1.SpringBoot 接口打包,并用jar包方式部署 简介:用jar包方式在控制台进行启动 打包 mvn package && java -jar target/gs-spring ...
- DOM3中的自定义事件
DOM3级还定义了自定义事件,自定义事件不是由DOM原生触发的,它的目的是让开发人员创建自己的事件.要创建的自定义事件可以由createEvent("CustomEvent"); ...
- Object-Oriented Programming Summary Ⅲ
目录 JML单元作业博客 1.1 梳理JML语言的理论基础 0. 前言 1. 形式 2. 作用域 3. 前置条件 (requires) 4. 后置条件 (ensures) 5. 模型域 (model) ...
- 使用 Redis 如何实现查询附近的人?「视频版」——面试突击 003 期
面试问题 Redis 如何实现查询附近的人? 涉及知识点 Redis 中如何操作位置信息? GEO 底层是如何实现的? 如何在程序实现查询附近的人? 在实际使用中需要注意哪些问题? 视频答案 视频地址 ...
- jwt的token如何使用
JWT简介: JWT(JSON WEB TOKEN):JSON网络令牌,JWT是一个轻便的安全跨平台传输格式,定义了一个紧凑的自包含的方式在不同实体之间安全传输信息(JSON格式).它是在Web环境下 ...