German Collegiate Programming Contest 2018 C. Coolest Ski Route
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\n",maxx); 79 return 0; 80 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现