[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的判断条件之类的
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 **/