我是靠谱客的博主 大力小蝴蝶,这篇文章主要介绍HDU - 6071 Lazy Running 同余最短路 + 分层,现在分享给大家,希望可以做个参考。

传送门

题意: 给定四个点构成一个环,给出四个点之间的距离,让后从 2 2 2号点出发,最终回到 2 2 2号点,求经过的距离 > = k >=k >=k的最小距离。

思路: 由于从 2 2 2开始,最终在 2 2 2结束,所以我们考虑 2 ∗ m i n ( g [ 1 ] [ 2 ] , g [ 2 ] [ 3 ] ) 2*min(g[1][2],g[2][3]) 2min(g[1][2],g[2][3])作为基数,乘二的原因是要出去再回来。让后以这个数为基数,跑同余最短路就行啦。因为这是一个图,需要将原本 d i s [ i ] dis[i] dis[i]改成 d i s [ i ] [ j ] dis[i][j] dis[i][j]表示到 i i i这个点的时候,模 b a s e base base之后为 j j j的时候的最短距离,让后每次到 2 2 2号点的时候更新答案即可。

复制代码
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
//#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid (tr[u].l+tr[u].r>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") using namespace std; //void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); } typedef long long LL; typedef unsigned long long ULL; typedef pair<int,LL> PII; const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6; LL k,base,ans=5e18; LL g[10][10]; LL dis[4][N];//到第i个点,距离%2*base为j的状态 bool st[4][N]; void spfa() { queue<PII>q; q.push({1,0ll}); memset(dis,INF,sizeof(dis)); st[1][0]=true; dis[1][0]=0; while(q.size()) { PII t=q.front(); q.pop(); LL u=t.X,d=t.Y; st[u][d]=false; if(u==1) { LL ds=dis[u][d]; if(ds>=k) ans=min(ans,ds); else { ans=min(ans,(k-ds)/base*base+((k-ds)%base==0? 0:base)+ds); } } for(int i=-1;i<=1;i+=2) { int ver=(u+i+4)%4; int now=(d+g[u][ver])%base; if(dis[ver][now]>dis[u][d]+g[u][ver]) { dis[ver][now]=dis[u][d]+g[u][ver]; if(!st[ver][now]) q.push({ver,now}),st[ver][now]=true; } } } } int main() { // ios::sync_with_stdio(false); // cin.tie(0); int _; scanf("%d",&_); while(_--) { cin>>k; ans=5e18; for(int i=0;i<4;i++) scanf("%d",&g[i][(i+1)%4]),g[(i+1)%4][i]=g[i][(i+1)%4]; base=2*min(g[1][0],g[1][2]); spfa(); printf("%lldn",ans); } return 0; } /* */

最后

以上就是大力小蝴蝶最近收集整理的关于HDU - 6071 Lazy Running 同余最短路 + 分层的全部内容,更多相关HDU内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(96)

评论列表共有 0 条评论

立即
投稿
返回
顶部