码迷,mamicode.com
首页 > 其他好文 > 详细

1003 Emergency

时间:2018-06-11 02:02:52      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:==   style   code   problems   tps   get   resize   ble   TE   

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

标签:==   style   code   problems   tps   get   resize   ble   TE   

原文地址:https://www.cnblogs.com/jinjin-2018/p/9165122.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!