题目描述  传送门

Bessie has taken heed of the evils of sloth and has decided to get fit by jogging from the barn to the pond several times a week. She doesn't want to work too hard, of course, so she only plans to jog downhill to the pond and then amble back to the barn at her leisure.

Bessie also doesn't want to jog any too far either, so she generally takes the shortest sequence of cow paths to get to the pond. Each of the M (1 <= M <= 10,000) cow paths connects two pastures

conveniently numbered 1..N (1 <= N <= 1000). Even more conveniently, the pastures are numbered such that if X>Y then the cow path from pasture X to pasture Y runs downhill. Pasture N is the barn (at the top of the hill) and pasture 1 is the pond (at the bottom).

Just a week into her regimen, Bessie has begun to tire of always taking the same route to get to the pond. She would like to vary her route by taking different cow paths on different days. Specifically, Bessie would like to take exactly K (1 <= K <= 100) different routes for variety. To avoid too much exertion, she wants these to be the K shortest routes from the barn to the pond. Two routes are considered different if they comprise different sequences of cow paths.

Help Bessie determine how strenuous her workout will be by determining the lengths of each of the K shortest routes on the network of pastures. You will be supplied a list of downhill cow paths from X_i to Y_i along with the cow path's length: (X_i, Y_i, D_i) where (1 <= Y_i < X_i; Y_i < X_i <= N). Cowpath i has length D_i (1 <= D_i <= 1,000,000).

贝西尝到了懒惰的恶果——为了减肥,她不得不决定每周花几次时间在牛棚和池塘之间慢跑。但贝西并不想太累,所以她打算只跑从牛棚到池塘的下坡路,然后再慢慢地从池塘走回牛棚。

同时,贝西也不想跑得太远,所以她只想沿着通向池塘的最短路径跑步。在牧场里,每条道路连接了两个结点(这些结点的编号为1到N,1≤N≤1000)。另外,如果X>?,说明结点X的地势要高于Y,所以下坡的道路是从X通向Y的,贝西所在牛棚的编号为N(最高点),池塘的编号为1(最低点)。

而然,一周之后,贝西对单调的路线厌倦了,她希望每天可以跑不同的路线,比如说,最好能有K (1≤K≤100)种不同的选择。为了不至于跑得太累,她希望这K条路径是从牛棚到池塘的最短的K条路径。

请帮助贝西算算她的运动量,即找出网络里最短的K条路径的长度。假设每条道路用(Xi,Yi,Di)表示,其中1≤Yi<Xi≤N,表示这条道路从Xi出发到Yi,其长度为Di (1≤Di≤1,000,000)。

输入格式

* Line 1: Three space-separated integers: N, M, and K

* Lines 2..M+1: Line i+1 describes a downhill cow path using three space-separated integers: X_i, Y_i, and D_i

输出格式

* Lines 1..K: Line i contains the length of the i-th shortest route or -1 if no such route exists. If a shortest route length occurs multiple times, be sure to list it multiple times in the output.

输入输出样例

输入 #1

5 8 7 5 4 1 5 3 1 5 2 1 5 1 1 4 3 4 3 1 1 3 2 1 2 1 1 

输出 #1

1 2 2 3 6 7 -1 

说明/提示

The routes are (5-1), (5-3-1), (5-2-1), (5-3-2-1), (5-4-3-1),

(5-4-3-2-1).

思路:K短路径模板题,先建立反向图,找到各个顶点到目的点的最短距离(可以用Dijkstra或者是spfa,看个人喜好了)。然后再用A*算法(广度优先搜索+加剪枝),一点点得出最短的前K条路经。 其中用于剪枝的评估函数是f(u) = dis0[u] + dis[u],其中 dis0[u] 表示:N~u的最短路径,dis[u] 表示 u~1的最短路径。根据评估函数的从小到大依次求出图的前K短路径.

AC代码:

#include <bits/stdc++.h>using namespace std;
#define io ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mset(a, n) memset(a, n, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<int,PII> PPI;
typedef pair<ll, ll> PLL;
const int maxn =  1e3+5;int V, E, K; int tot;
int dis[maxn];
bool vis[maxn];struct node{int to;int val;node(){}node(int _to, int _val): to(_to), val(_val){}bool operator < (const node &a) const {return val+dis[to] > a.val+dis[a.to];}
};
struct Edges{int to;int val;Edges(){}Edges(int _to, int _val): to(_to), val(_val){}
};
vector<Edges> G[maxn], lG[maxn];
int ans[maxn];void addedge(int u, int v, int val){G[u].pb(Edges(v, val));lG[v].pb(Edges(u, val));
}void spfa(){mset(vis,false);mset(dis,0x3f3f);queue<int> Q;Q.push(1);vis[1] = true;dis[1] = 0;while(!Q.empty()){int u = Q.front(); Q.pop();vis[u] = false;for(int i=0;i<lG[u].size();i++){int v = lG[u][i].to, val = lG[u][i].val;if(dis[v] > dis[u]+val){dis[v] = dis[u]+val;if(!vis[v]){Q.push(v);vis[v] = true;}}}}}void a_star(){priority_queue<node> Q;Q.push(node(V,0));while(!Q.empty()){node nw = Q.top(); Q.pop();if(nw.to == 1){tot++;ans[tot] = nw.val;if (tot>K) return;}for(int i = 0; i<G[nw.to].size();i++){Q.push(node(G[nw.to][i].to, nw.val+G[nw.to][i].val));}}return;
}int main(){scanf("%d%d%d", &V, &E, &K);for(int i=1;i<=E;i++){int u,v,val;scanf("%d%d%d", &u, &v, &val);addedge(u,v,val);}mset(ans,-1);spfa();tot = 0;a_star();for(int i=1;i<=K;i++){printf("%d\n", ans[i]);}return 0;
}

P2901 [USACO08MAR]牛慢跑Cow Jogging相关推荐

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

    题目链接:https://daniu.luogu.org/problem/show?pid=2901 Astar的方程$f(n)=g(n)+h(n)$,在这道题中我们可以反向最短路处理出$h(n)$的 ...

  2. 洛谷 P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver

    P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题目描述 The cows are out exercising their hooves again! There are N ...

  3. 洛谷 P3014 [USACO11FEB]牛线Cow Line

    P3014 [USACO11FEB]牛线Cow Line 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 The N (1 <= N <= 20) ...

  4. linux内核通用提权漏洞expliot 脏牛Dirty COW

    0x01 漏洞简介 Linux内核在处理内存写时拷贝(Copy-on-Write)时存在条件竞争漏洞,导致可以破坏私有只读内存映射.一个低权限的本地用户能够利用此漏洞获取其他只读内存映射的写权限,有可 ...

  5. [USACO07NOV]牛继电器Cow Relays

    题目描述 给出一张无向连通图,求S到E经过k条边的最短路. 输入输出样例 输入样例#1: 2 6 6 4 11 4 6 4 4 8 8 4 9 6 6 8 2 6 9 3 8 9 输出样例#1: 10 ...

  6. P2419 [USACO08JAN]牛大赛Cow Contest

    Floyd不仅能求出最短路,还能利用或运算,与运算判断两点是否连通. 小范围数据,Floyd经常是很好的思路! 代码! #include<cstdio> #include<iostr ...

  7. [LUOGU] P2886 [USACO07NOV]牛继电器Cow Relays

    https://www.luogu.org/problemnew/show/P2886 给定无向连通图,求经过k条边,s到t的最短路 Floyd形式的矩阵乘法,同样满足结合律,所以可以进行快速幂. 离 ...

  8. [USACO08JAN]牛大赛Cow Contest

    传送门:https://www.luogu.org/problemnew/show/P2419 这道题拿到之后想了很多种做法,贪心....以及建树...然后....从入度为零的点求最短路....然鹅好 ...

  9. [USACO12FEB]牛券Cow Coupons

    嘟嘟嘟 这其实是一道贪心题,而不是dp. 首先我们贪心的取有优惠券中价值最小的,并把这些东西都放在优先队列里,然后看[k + 1, n]中,有些东西使用了优惠券减的价钱是否比[1, k]中用了优惠券的 ...

最新文章

  1. java jms clust,Geoserver 的 JMS Cluster modules(集群数据同步)
  2. Android USB转串口开发(hoho.android.usbserial串口库)
  3. 快速入门虚拟机+linux安装(附带视频)
  4. 一文看懂数据挖掘:哪一种方法最好?都需要哪些技术?
  5. 关于pchunter1.57x64过期使用
  6. java二叉树求权值_百度笔试题目:二叉树路径权值和【转】
  7. 【运维安全】-MySQL手工注入
  8. include包含文件查找的顺序 .
  9. phpdesigner 7 key注册码
  10. NDK开发(八) :JNI下Bitmap的使用
  11. php上传带进度条_PHP+Ajax无刷新带进度条图片上传示例
  12. 计算机里的文档怎么设置密码,文件夹怎么设置密码,教您如何给电脑上文件夹设置密码...
  13. 十门峡旅游攻略:临安十门峡的春天
  14. APP统计报表,这几个指标最有价值
  15. shoppingResult 客户端结算
  16. 事物以及事物隔离性的代码详解
  17. 蚂蚁金服面试经验分享
  18. mysql5.7.10 64_mysql5.7.10win764安装
  19. 基于Android公交查询系统的设计与实现(论文+程序设计源码+数据库文件)
  20. Java实现UDP协议

热门文章

  1. java对接 布防 海康威视_java web整合海康威视录像机摄像SDK
  2. 使用旋转动画实现刻度表
  3. SpringBoot整合Graylog3.0
  4. kubernetes Pod Lifecycle生命周期与livenessProbe、 readinessProbe探测方法
  5. html中微博发布怎么做,js实现微博发布小功能
  6. u盘误删文件怎么恢复
  7. 打印Service运行时间与Aspect相关注解使用
  8. 2022年人工智能领域发展七大趋势
  9. 经常失眠怎么办?这些方法和好物可以帮到你
  10. 机器学习入门系列之PCA降维