1 John loves winter. Every skiing season he goes heli-skiing with his friends. To do so, they rent a helicopter that flies them directly to any mountain in the Alps. From there they follow the picturesque slopes through the untouched snow. 2 3 Of course they want to ski on only the best snow, in the best weather they can get. For this they use a combined condition measure and for any given day, they rate all the available slopes. 4 5 Can you help them find the most awesome route? 6 7 Input Format 8 The input consists of: 9 10 one line with two integers nn (2 le n le 1000)(2≤n≤1000) and mm (1 le m le 5000)(1≤m≤5000),where nn is the number of (11-indexed) connecting points between slopes and mm is the number of slopes. 11 mm lines, each with three integers ss,tt,cc (1 le s,t le n, 1 le c le 100)(1≤s,t≤n,1≤c≤100) representing a slopefrom point ss to point tt with condition measure cc. 12 Points without incoming slopes are mountain tops with beautiful scenery, points without outgoing slopes are valleys. The helicopter can land on every connecting point, so the friends can start and end their tour at any point they want. All slopes go downhill, so regardless of where they start, they cannot reach the same point again after taking any of the slopes. 13 14 Output Format 15 Output a single number nn that is the maximum sum of condition measures along a path that the friends could take 16 17 Hint 18 19 20 样例输入1 复制 21 5 5 22 1 2 15 23 2 3 12 24 1 4 17 25 4 2 11 26 5 4 9 27 样例输出1 复制 28 40 29 样例输入2 复制 30 6 6 31 1 2 2 32 4 5 2 33 2 3 3 34 1 3 2 35 5 6 2 36 1 2 4 37 样例输出2 复制 38 7 39 题目来源 40 German Collegiate Programming Contest 2018
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 #include <algorithm> 6 #include <utility> 7 #include <vector> 8 #include <map> 9 #include <queue> 10 #include <stack> 11 #include <cstdlib> 12 #include <cmath> 13 typedef long long ll; 14 #define lowbit(x) (x&(-x)) 15 #define ls l,m,rt<<1 16 #define rs m+1,r,rt<<1|1 17 using namespace std; 18 #define pi acos(-1) 19 #define P pair<ll,ll> 20 const int N =5e3+100;//开始把B设为了1000,一直 段错误,m <=5000 21 const ll inf =1e12+100; 22 int n,m; 23 ll u,v,w,cnt; 24 ll d[N],head[N]; 25 struct Edge{ 26 ll fr,to,val,nex; 27 Edge(){} 28 Edge(ll fr,ll to,ll val,ll nex):fr(fr),to(to),val(val),nex(nex){} 29 }e[N*2]; 30 void add(ll u,ll v,ll w){ 31 e[cnt].fr=u; 32 e[cnt].to=v; 33 e[cnt].val=w; 34 e[cnt].nex=head[u]; 35 head[u]=cnt++; 36 } 37 void init() 38 { 39 for(int i =0;i<N;i++){ 40 d[i] = 0;//求最大值 41 head[i] = -1; 42 } 43 cnt = 0; 44 } 45 void bfs() 46 { 47 priority_queue<P,vector<P>,greater<P> >Q; 48 for(int i =1;i<=n;i++) 49 Q.push(P(0,i));//终点为I的路线的距离为0(i到i) 50 while(!Q.empty()) 51 { 52 P tmp = Q.top(); 53 Q.pop(); 54 ll v= tmp.second; 55 if(d[v] > tmp.first) continue;//距离短的路线不用走了。 56 for(ll i=head[v];i!=-1;i=e[i].nex){ 57 Edge ee =e[i]; 58 ll t= e[i].to; 59 if(d[t]<d[v]+ee.val){ 60 d[t]=d[v]+ee.val; 61 Q.push(P(d[t],t)); 62 } 63 } 64 65 } 66 } 67 int main() 68 { 69 scanf("%d%d",&n,&m); 70 init(); 71 for(int i=0;i<m;i++){ 72 scanf("%lld%lld%lld",&u,&v,&w); 73 add(u,v,w);//有向图 74 } 75 bfs(); 76 ll maxx = -inf; 77 for(int i =1;i<=n;i++) maxx=max(maxx,d[i]);//找到一条路线距离最长 78 printf("%lld ",maxx); 79 return 0; 80 }