[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,每一个节点的深度为第一个与他相连的节点的深度+1,则在计算每个点的最小代价时只能从深度和他相差1的节点更新,这样才能保证离源点最近
否则你就会一直卡在test9,还有注意判断相等要用==,一开始我想到了记录深度的问题,结果还是WA到自闭,结果才发现判断相等的==写成了=,以后注意要多检查if,while的判断条件之类的

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const ll amn=105,inf=1e18;
 5 struct node{
 6     ll i,w;
 7     node(ll ii,ll ww){i=ii,w=ww;}
 8 };
 9 vector<node> eg[amn];
10 queue<int> q;
11 string nu,nv;
12 map<string,int> mp;
13 ll n,m,cost[amn],deep[amn];
14 void bfs(int s){
15     deep[s]=0;                                      ///源点深度初始化为0
16     while(q.size())q.pop();
17     q.push(s);
18     while(q.size()){
19         int u=q.front();q.pop();
20         for(int i=0;i<eg[u].size();i++){
21             ll v=eg[u][i].i,w=eg[u][i].w;
22             if(deep[v]==inf)deep[v]=deep[u]+1;      ///如果是第一次访问就把当前节点的深度设为父节点的深度+1
23             if(deep[v]==deep[u]+1){                 ///当前节点为父节点的深度+1时才能更新代价最小值并加入搜索队列
24                 cost[v]=min(cost[v],w);
25                 q.push(v);
26             }
27         }
28     }
29 }
30 int main(){
31     ios::sync_with_stdio(0);
32     cin>>n>>m;
33     mp["English"]=0;            ///English作为源点编号为0
34     for(int i=1;i<=n;i++){
35         deep[i]=cost[i]=inf;
36         cin>>nu;
37         mp[nu]=i;               ///给每种语言作为节点编号
38     }
39     int w;
40     for(int i=0;i<m;i++){
41         cin>>nu>>nv>>w;
42          node u(mp[nu],w),v(mp[nv],w);
43          eg[u.i].push_back(v);
44          eg[v.i].push_back(u);
45     }
46     bfs(0);                     ///从源点开始bfs
47     bool valid=1;
48     ll ans=0;
49     for(int i=1;i<=n;i++){
50         if(cost[i]==inf){valid=0;break;}        ///如果碰到代价为inf的点,也就是说明源点无法到这个点,则说明情况非法,要输出Impossible
51         ans+=cost[i];                           ///记录最小代价之和
52     }
53     if(valid)
54         printf("%lld\n",ans);
55     else
56         printf("Impossible\n");
57 }
58 /**
59 以Englis为源点,并再给出n个点和m条边,并给出这n个点的名字,m条边中会给出两个邻接点和边的权值(边是无向边),问从源点开始是否能到达所有边,如果能到达输出离源点最近路径中的最小代价之和,否则输出Impossible
60 此题的坑点在于最小代价之和是需要在离源点最近路径中找,也就是说若设源点的深度为0,每一个节点的深度为第一个与他相连的节点的深度+1,则在计算每个点的最小代价时只能从深度和他相差1的节点更新,这样才能保证离源点最近
61 否则你就会一直卡在test9,还有注意判断相等要用==,一开始我想到了记录深度的问题,结果还是WA到自闭,结果才发现判断相等的==写成了=
62 **/

原文地址:https://www.cnblogs.com/Railgun000/p/11385282.html

时间: 2024-11-09 16:39:06

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

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

A .Abstract Art 题意:求多个多边形的面积并. 思路:模板题. #include<bits/stdc++.h> using namespace std; typedef long long ll; const double inf=1e200; const double eps=1e-12; const double pi=4*atan(1.0); int dcmp(double x){ return fabs(x)<eps?0:(x<0?-1:1);} struct

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

MPI Maelstrom 总时间限制:  1000ms 内存限制:  65536kB 描述 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor

East Central North America Region 2015

E 每过一秒,当前点会把它的值传递给所有相邻点,问t时刻该图的值 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <list> 5 #include <map> 6 #include <stack> 7 #include <vector> 8 #include <cstring> 9 #include <

2015 UESTC Winter Training #6【Regionals 2010 &gt;&gt; North America - Rocky Mountain】

2015 UESTC Winter Training #6 Regionals 2010 >> North America - Rocky Mountain A - Parenthesis 给一个长度不多于1000的表达式,只包含小写字母,加法运算,省略乘号的乘法运算和括号,输出去掉多余括号的表达式 括号匹配可以使用栈操作,只有两种情况可以去掉这一对括号: 左括号的左边是左边界.加法符号.左括号,并且右括号右边是有右边界.加法符号.右括号 如果括号内没有加法运算(括号内的括号里,也就是下一级括

That Diesel powered running watches north america doesn&#39;t need shift and legal requirements within any specific time

The form belonging to the Diesel's court case is totally distinct in addition to being very unlikely to find that remarkable high-quality because of shots solely. That face belonging to the Diesel powered is without a doubt everything that might be l

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Krak&#243;w

ACM ICPC Central Europe Regional Contest 2013 Jagiellonian University Kraków Problem A: Rubik's RectangleProblem B: What does the fox say?Problem C: Magical GCDProblem D: SubwayProblem E: EscapeProblem F: DraughtsProblem G: History courseProblem H: C

迷宫bfs+路径记录

给定5*5的地图,1表示墙,0表示空地,从左上角走到左下角 是把所有的可走路径都记录下来了,但是 搜索有递归与非递归形式 本代码使用非递归形式 bfs+路径记录 对于num[i].pre=head的理解是他存在很多条路,每个点是从上一个点走过来的,但总存在一条路是到达终点的,所以,只需要得到到终点的一个head就可以顺着这个路径标记回去了 #include <iostream> #include <cstdio> using namespace std; char a[10][10

2015 UESTC Winter Training #8【The 2011 Rocky Mountain Regional Contest】

2015 UESTC Winter Training #8 The 2011 Rocky Mountain Regional Contest Regionals 2011 >> North America - Rocky Mountain 开始时貌似是UVAlive挂了,无论交什么都WA,后来转战HDU 这次水题比较多,其中B题据说有更加高级的方法. G题WA了两发,才发现竟然没有输出Case!!! 未完成:D F H I J A - Iterated Difference 水题,模拟迭代即可

2018 ICPC Greater New York Regional Contest E What time is it anyway?(暴搜)

What time is it anyway? 524288K The \text{Frobozz Magic Clock Company}Frobozz Magic Clock Company makes 12-hour analog clocks that are always circular and have an hour hand and a minute hand as shown below: The shop has NN clocks all possibly showing