P1078 [NOIP2012 普及组] 文化之旅
https://www.luogu.com.cn/problem/P1078
搜索,图论,剪枝,最短路
搜索,图论,剪枝,最短路
绿色题
思路一:搜索
1.输入,建边,用一个数组存储已经学习的文化,
2.搜索,以当前的点now去看能走到哪些边,然后对连上的点判断是否学过要去的点的文化,或要去的点排斥自己已经学过的文化的任意一种
3.剪枝方法一: 如果当前长度>=ans ,就直接return (这样还是会TLE掉一个点(92分))
4.剪枝方法二: 可以直接开一个数组存储从起点到图任意一点的最短路程,如果当前路程大于到当前点的最短路程就直接return (这样做可以拿满分)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | <div id= "mCSB_1" class = "mCustomScrollBox mCS-minimal-dark mCSB_vertical_horizontal mCSB_outside" style= "max-height: none;" tabindex= "0" ><div id= "mCSB_1_container" class = "mCSB_container mCS_y_hidden mCS_no_scrollbar_y mCS_x_hidden mCS_no_scrollbar_x" style= "position: relative; top: 0px; left: 0px; width: 100%;" dir= "ltr" ><code-pre class = "code-pre" id= "pre-3kY5Rn" ><code-line class = "line-numbers-rows" ></code-line><span class = "hljs-meta" >#<span class = "hljs-meta-keyword" >include</span><span class = "hljs-meta-string" ><bits/stdc++.h></span></span> <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-keyword" > using </span> <span class = "hljs-keyword" > namespace </span> <span class = "hljs-built_in" >std</span>; <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-keyword" > const </span> <span class = "hljs-keyword" > int </span> maxn=<span class = "hljs-number" >105</span>; <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-keyword" > int </span> n,k,m,s,t,c[maxn],a[maxn][maxn],tot,cnt,whk[maxn],ans=<span class = "hljs-number" >1e9</span>,p[maxn][maxn],flag2=<span class = "hljs-number" >0</span>,ssum[maxn]; <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-keyword" > bool </span> flag; <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-comment" > /*struct edge{ </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment"> int dis,to,next; </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment">}e[maxn]; </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment">void add(int u,int v,int w) </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment">{ </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment"> tot++; </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment"> e[tot].next=head[u]; </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment"> e[tot].to=v; </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment"> head[u]=tot; </span><code-line class="line-numbers-rows"></code-line><span class="hljs-comment">}*/ </span> <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-function" ><span class = "hljs-keyword" > void </span> <span class = "hljs-title" >dfs</span><span class = "hljs-params" >(<span class = "hljs-keyword" > int </span> now,<span class = "hljs-keyword" > int </span> sum)</span> </span><code-line class = "line-numbers-rows" ></code-line>{ <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(sum>=ssum[now]) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > return </span>; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> ssum[now]=sum; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(now==t) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> flag2=<span class = "hljs-number" >1</span>; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > return </span>; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> i=<span class = "hljs-number" >1</span>; i<=n; i++) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(p[now][i]><span class = "hljs-number" >0</span>) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> flag=<span class = "hljs-number" >1</span>; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> j=<span class = "hljs-number" >1</span>; j<=cnt; j++) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(a[c[i]][whk[j]]==<span class = "hljs-number" >1</span>) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> flag=<span class = "hljs-number" >0</span>; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > break </span>; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(flag==<span class = "hljs-number" >1</span>) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> whk[++cnt]=c[i]; <code-line class = "line-numbers-rows" ></code-line> dfs(i,sum+p[now][i]); <code-line class = "line-numbers-rows" ></code-line> cnt--; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > return </span>; <code-line class = "line-numbers-rows" ></code-line>} <code-line class = "line-numbers-rows" ></code-line><span class = "hljs-function" ><span class = "hljs-keyword" > int </span> <span class = "hljs-title" >main</span><span class = "hljs-params" >()</span> </span><code-line class = "line-numbers-rows" ></code-line>{ <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-built_in" >cin</span>>>n>>k>>m>>s>>t; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> i=<span class = "hljs-number" >1</span>; i<=n; i++) ssum[i]=<span class = "hljs-number" >1e9</span>; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> i=<span class = "hljs-number" >1</span>; i<=n; i++) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-built_in" >cin</span>>>c[i]; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> i=<span class = "hljs-number" >1</span>; i<=k; i++) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> j=<span class = "hljs-number" >1</span>; j<=k; j++) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-built_in" >cin</span>>>a[i][j]; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> a[i][i]=<span class = "hljs-number" >1</span>; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > for </span>(<span class = "hljs-keyword" > int </span> i=<span class = "hljs-number" >1</span>; i<=m; i++) <code-line class = "line-numbers-rows" ></code-line> { <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > int </span> u,v,d; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-built_in" >cin</span>>>u>>v>>d; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(p[u][v]==<span class = "hljs-number" >0</span>||d<p[u][v]) p[u][v]=d; <code-line class = "line-numbers-rows" ></code-line> p[v][u]=d; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> cnt=<span class = "hljs-number" >1</span>; whk[cnt]=c[s]; <code-line class = "line-numbers-rows" ></code-line> dfs(s,<span class = "hljs-number" >0</span>); <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > if </span>(flag2==<span class = "hljs-number" >1</span>) <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-built_in" >cout</span><<ssum[t]<<<span class = "hljs-built_in" >endl</span>; <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > else </span>{ <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-built_in" >cout</span><<<span class = "hljs-number" >-1</span><<<span class = "hljs-built_in" >endl</span>; <code-line class = "line-numbers-rows" ></code-line> } <code-line class = "line-numbers-rows" ></code-line> <span class = "hljs-keyword" > return </span> <span class = "hljs-number" >0</span>; <code-line class = "line-numbers-rows" ></code-line>} </code-pre></div></div><div id= "mCSB_1_scrollbar_vertical" class = "mCSB_scrollTools mCSB_1_scrollbar mCS-minimal-dark mCSB_scrollTools_vertical" style= "display: none;" ><div class = "mCSB_draggerContainer" ><div id= "mCSB_1_dragger_vertical" class = "mCSB_dragger" style= "position: absolute; min-height: 0px; height: 0px; top: 0px;" ><div class = "mCSB_dragger_bar" style= "line-height: 0px; background-color: rgb(0, 0, 0);" ></div></div><div class = "mCSB_draggerRail" ></div></div></div><div id= "mCSB_1_scrollbar_horizontal" class = "mCSB_scrollTools mCSB_1_scrollbar mCS-minimal-dark mCSB_scrollTools_horizontal" style= "display: none;" ><div class = "mCSB_draggerContainer" ><div id= "mCSB_1_dragger_horizontal" class = "mCSB_dragger" style= "position: absolute; min-width: 0px; width: 0px; left: 0px;" ><div class = "mCSB_dragger_bar" style= "background-color: rgb(0, 0, 0);" ></div></div><div class = "mCSB_draggerRail" ></div></div></div> |
__EOF__

本文作者:灰の魔女伊蕾娜
本文链接:https://www.cnblogs.com/2elaina/p/16721127.html
关于博主:编程小萌新一名,希望从今天开始慢慢提高,一步步走向技术的高峰!
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/2elaina/p/16721127.html
关于博主:编程小萌新一名,希望从今天开始慢慢提高,一步步走向技术的高峰!
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战