[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 题意: 通过一棵二叉树的中序 ...
随机推荐
- BeWhatever
Hadoop Distributed File System:分布式文件系统. HDFS基于流数据模式访问和处理超大文件需求开发,具有高容错性,高可靠性,高可扩展性,多部署在低成本的硬件上.HDFS提 ...
- 机器CPU load过高问题排查
load average的概念 系统平均负载定义:在特定时间间隔内运行队列中(在CPU上运行或者等待运行多少进程)的平均进程数.如果一个进程满足以下条件则其就会位于运行队列中: 它没有在等待I/O操作 ...
- selenium之浏览器、元素、鼠标等操作总结
1 控制浏览器 Selenium 主要提供的是操作页面上各种元素的方法,但它也提供了操作浏览器本身的方法,比如浏览器的大小以及浏览器后退.前进按钮等. 1.1 控制浏览器窗口大小 在不同的浏览 ...
- 图示JVM工作原理
JDK,JRE,JVM的联系是啥? JVM Java Virtual Machine JDK Java Development Kit JRE Java Runtime Environment 看上图 ...
- Centos +Docker 安装及仓库使用概述
1. Linux 系统学习Docker安装篇 这里我使用的Centos系统 安装Docker yum命令说明 即Yellowdog Update Modifier,是一种基于rpm的包管理工具 yu ...
- 用 Python 生成 HTML 表格
在 邮件报表 之类的开发任务中,需要生成 HTML 表格. 使用 Python 生成 HTML 表格基本没啥难度, for 循环遍历一遍数据并输出标签即可. 如果需要实现合并单元格,或者按需调整表格样 ...
- 从HTML标签开始
开始这一切吧! 没错,你没看错,我将从HTML标签开始我的整个系列文章.很基础吧?但是每个前端人都是从最简单的HTML标签开始的,都是从一个<html></html>开始整个前 ...
- LeetCode 162.Find Peak Element(M)(P)
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- Windows下利用virtualenvwrapper指定python版本创建虚拟环境
默认已安装virtualenvwrapper 一.添加环境变量(可选) 在系统环境变量中添加 WORKON_HOME ,用来指定新建的虚拟环境的存储位置,如过未添加,默认位置为 %USERPROFIL ...
- PTP从时钟授时模块应用及介绍
PTP从时钟授时模块应用及介绍 随着网络技术的不断进步和发展,NTP网络时间协议已经满不了一些精密设备和仪器的精度要求,这时就需要精度更高的PTP协议,PTP协议是一种应用于分布式测量和控制系统中的精 ...