[Luogu2901][USACO08MAR]牛慢跑Cow Jogging Astar K短路

题目链接:https://daniu.luogu.org/problem/show?pid=2901

Astar的方程$f(n)=g(n)+h(n)$,在这道题中我们可以反向最短路处理出$h(n)$的精确值。然后跑Astar找K次最短路就好了。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 using namespace std;
 6 int inline readint(){
 7     int Num;char ch;
 8     while((ch=getchar())<‘0‘||ch>‘9‘);Num=ch-‘0‘;
 9     while((ch=getchar())>=‘0‘&&ch<=‘9‘) Num=Num*10+ch-‘0‘;
10     return Num;
11 }
12 int N,M,K;
13 int to[10010],ne[10010],w[10010],fir[1010],cnt=0;
14 void add(int a,int b,int c){
15     to[++cnt]=b;
16     w[cnt]=c;
17     ne[cnt]=fir[a];
18     fir[a]=cnt;
19 }
20 int nto[10010],nne[10010],nw[10010],nfir[1010],ncnt=0;
21 void nadd(int a,int b,int c){
22     nto[++ncnt]=b;
23     nw[ncnt]=c;
24     nne[ncnt]=nfir[a];
25     nfir[a]=ncnt;
26 }
27 int dis[1010];
28 bool in[1010];
29 queue <int> q;
30 void Spfa(){
31     memset(dis,127/3,sizeof(dis));
32     in[1]=true;
33     dis[1]=0;
34     q.push(1);
35     int u;
36     while(!q.empty()){
37         int u=q.front();
38         q.pop();
39         in[u]=false;
40         for(int i=nfir[u];i!=-1;i=nne[i]){
41             int v=nto[i];
42             if(dis[v]>dis[u]+nw[i]){
43                 dis[v]=dis[u]+nw[i];
44                 if(!in[v]){
45                     in[v]=true;
46                     q.push(v);
47                 }
48             }
49         }
50     }
51 }
52 struct NODE{
53     int d,num;
54     NODE(int _d=0,int _num=0){
55         d=_d;
56         num=_num;
57     }
58     bool operator < (const NODE &_)const{
59         return d>_.d;
60     }
61 };
62 int ans[110],rk=0;
63 priority_queue <NODE> Q;
64 void Astar(){
65     Q.push(NODE(dis[N],N));
66     NODE u;
67     while(!Q.empty()){
68         u=Q.top();
69         Q.pop();
70         if(u.num==1){
71             ans[++rk]=u.d;
72             if(rk==K) return;
73         }
74         for(int i=fir[u.num];i!=-1;i=ne[i])
75             Q.push(NODE(u.d-dis[u.num]+w[i]+dis[to[i]],to[i]));
76     }
77 }
78 int main(){
79     memset(fir,-1,sizeof(fir));
80     memset(nfir,-1,sizeof(nfir));
81     N=readint();
82     M=readint();
83     K=readint();
84     for(int i=1;i<=M;i++){
85         int a=readint(),
86             b=readint(),
87             c=readint();
88         add(a,b,c);
89         nadd(b,a,c);
90     }
91     Spfa();
92     Astar();
93     for(int i=1;i<=K;i++)
94         if(ans[i]) printf("%d\n",ans[i]);
95         else puts("-1");
96     return 0;
97 }
时间: 2024-10-26 21:06:52

[Luogu2901][USACO08MAR]牛慢跑Cow Jogging Astar K短路的相关文章

洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at

P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

题目描述 The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position on the track, and some cows jog at different speeds. With only one lane

洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

传送门 题目大意:n头牛在单行道n个位置,开始用不同的速度跑步. 当后面的牛追上前面的牛,后面的牛会和前面的牛以一样的速度 跑,称为一个小团体.问:ts后有多少个小团体. 题解:模拟 倒着扫一遍,因为某头牛后面的牛对这头牛的速度没影响. 计算出ts后牛的终点,如果能撞上前面最慢的牛,那么小团体数+1 注意开long long 一开始不理解为什么倒着扫, 因为如果正着扫,看第i头牛能否撞上i+1头, 我们不确定第i+1头牛的速度,可能第i+1头牛 速度很快,追上i+2头牛速度减缓,从而被第i头牛追

【Luogu】P2901牛慢跑(K短路模板)

题目链接 K短路居然用A*……奇妙. 先建反图从终点(1)跑一遍最短路,再A*,用堆存当前点到终点距离+从起点到当前点距离. 每次取出终点都可以视为发现了一个新的最短路. #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cctype> #include<queue> #define maxn 1020 #define m

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget

bzoj 1598: [Usaco2008 Mar]牛跑步 -- 第k短路,A*

1598: [Usaco2008 Mar]牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MB Description BESSIE准备用从牛棚跑到池塘的方法来锻炼. 但是因为她懒,她只准备沿着下坡的路跑到池塘, 然后走回牛棚. BESSIE也不想跑得太远,所以她想走最短的路经. 农场上一共有M (1 <= M <= 10,000)条路, 每条路连接两个用1..N(1 <= N <= 1000)标号的地点. 更方便的是,如果X>Y,则地点X

洛谷 P3014 [USACO11FEB]牛线Cow Line

P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Far

[USACO12FEB]牛券Cow Coupons(堆,贪心)

[USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget of M units of money (1 <= M <= 10^14). Cow i costs P_i money (1 <= P_i <= 10^9), b

aStar算法求第k短路

A*的概念主意在于估计函数,f(n)=g(n)+h(n),f(n)是估计函数,g(n)是n节点的当前代价,h(n)是n节点的估计代价:而实际中,存在最优的估计函数f'(n)=g'(n)+h'(n),那么显然我们在A*的估计中,h(n)<=h'(n),否则你将搜不到最优解:(g(n)>=g'(n)我还不怎么清楚为什么啊,大多数情况是g(n)=g'(n),这个可以不用管吧..) 求s->t的第k短路, dist[x]为x->t的最短路 (这个只要反向建边,然后一次spfa,求出任意点到