1003 Emergency
Posted jinjin-2018
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1003 Emergency相关的知识,希望对你有一定的参考价值。
1003 Emergency
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376
思路:DFS,到达终点停止,或超过现有方案的最短路停止。
注意点:distance是关键词;找到新的最短路时更新maxteams.
1 #include<iostream> 2 #include<vector> 3 #include<map> 4 #include<utility> 5 using namespace std; 6 7 vector<vector<pair<int,int>> > _distance; 8 vector<int> teams; 9 vector<int> seen; 10 double mind = 6E10; 11 int minn = 0; 12 int maxteams = 0; 13 14 void DFS(int c1,int c2,double dnow,int tnow ){ 15 if(dnow > mind ){ //剪枝 16 return; 17 } 18 if(c1 == c2){ //到终点 19 if(dnow < mind){ 20 minn = 1; 21 mind = dnow; 22 maxteams = tnow; 23 }else { 24 minn++; 25 maxteams = maxteams > tnow? maxteams:tnow; 26 } 27 28 return; 29 } 30 seen[c1] = 1; 31 for(pair<int,double> i : _distance[c1] ){ 32 if(seen[i.first] == 0){ 33 DFS(i.first,c2,dnow+i.second,tnow+teams[i.first]); 34 } 35 } 36 seen[c1] = 0; 37 return; 38 } 39 40 int main(){ 41 int n,m,c1,c2; 42 cin>>n>>m>>c1>>c2; 43 _distance.resize(n); 44 for(int i = 0; i < n; i++){ 45 int temp_teams; 46 cin>>temp_teams; 47 teams.push_back(temp_teams); 48 seen.push_back(0); 49 } 50 for(int i = 0; i < m; i++){ 51 int a,b; 52 double d; 53 cin>>a>>b>>d; 54 _distance[a].push_back(make_pair(b,d)); 55 _distance[b].push_back(make_pair(a,d)); 56 } 57 DFS(c1,c2,0,teams[c1]); 58 cout<<minn<<‘ ‘<<maxteams; 59 return 0; 60 }
以上是关于1003 Emergency的主要内容,如果未能解决你的问题,请参考以下文章