[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 题意: 通过一棵二叉树的中序 ...
随机推荐
- Janet Wu price
上次也是第一次参加百公里是2012的时候,那年的主题是一路有你,和一群同事从深圳湾走到福田,最后累了就回家了,那晚应该是睡得很好吧. 今年是提前报名了,虽然还不确定是否参加.因为说实话,我不喜欢拥堵的 ...
- 高阶函数---swift中的泛型介绍(一步步实现Map函数)
说明 本文内容均出自函数式 Swift一书, 此处整理仅仅是为了自己日后方便查看, 需要深入研究的话, 可以点进去购买, 支持原作者 本书由 王巍–新浪微博大神翻译 OneV's Den 喵神博客 接 ...
- 笔记: SpringBoot + VUE实现数据字典展示功能
最近一直在写前端,写得我贼难受,从能看懂一些基础的代码到整个前端框架撸下来鬼知道我经历了啥(:´д`)ゞ 项目中所用到的下拉菜单的值全部都是有数据库中的数据字典表来提供的,显示给用户的是的清晰的意思, ...
- Azure Devops测试管理(上)
因为最近测试人员合并到我这边开发组,对于如何能更好管理测试流程和测试与开发能更高效的完成任务,通俗的说如何能更敏捷,深入思考,然后就开始琢磨起TFS(也称之为VSTS/Azure Devops,因为我 ...
- 爬虫(二)requests 登陆某检索网站
1 import requests import os from PIL import Image import pytesseract import re rootUrl = xxx # 构建登录页 ...
- Asp.Net Core Filter 深入浅出的那些事-AOP
一.前言 在分享ASP.NET Core Filter 使用之前,先来谈谈AOP,什么是AOP 呢? AOP全称Aspect Oriented Programming意为面向切面编程,也叫做面向方法编 ...
- FC及BFC
1.什么是FC 2.BFC块级格式化上下文(Block formatting context) Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很多个 Box 组成的.元素的类 ...
- HTML5 Canvas(基础知识)
最近笔者在学习HTML5的新元素<canvas>,会分享一些基础知识以及小例子,最终使用<canvas>实现一个绘制简单图表(条形图.线图或者饼图)的js库,会更新一到两篇文章 ...
- Eclipse+Mysql实现多条件查询
最近做一个项目的时候,就需要用到多条件查询,但是一直不完美,所有有bug,不过今天经高人提醒,做出了个小例子,在这里简单跟大家分享一下: 不说多了,直接放关键sql代码: 已知条件:菜名,菜品,价格区 ...
- Javascript中String()和new String()的区别——JS的包装对象
最近在看Symbol不能使用new操作符,然后类比到Number,String,Boolean,因为它们同属于基本类型,但是有有所差异:Number,String,Boolean是可以使用new操作符 ...