题目描述

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).

图论 A* 最短路

突然就忘了重载优先级怎么写

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,w;
}e[mxn<<],ve[mxn<<];
int hd[mxn],mct=;
int vd[mxn],vct=;
void add_edge(int u,int v,int w){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;
ve[++vct].v=u;ve[vct].nxt=vd[v];ve[vct].w=w;vd[v]=vct;
return;
}
struct node{
int u,dis;
node(int a,int b){u=a;dis=b;}
friend bool operator < (const node &a,const node &b){
return a.dis>b.dis;
}
};
priority_queue<node>q;
int dis[mxn];
int n,m,K;
void Dij(){
memset(dis,0x3f,sizeof dis);
dis[n]=;q.push(node(n,));
while(!q.empty()){
node U=q.top();q.pop();
while(U.dis>dis[U.u]){if(q.empty())break;U=q.top();q.pop();}
int u=U.u;
// printf("u:%d\n",u);
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
q.push((node){v,dis[v]});
}
}
}
return;
}
int tot=;
struct cmp{
bool operator () (const node a,const node b){
return a.dis+dis[a.u]>b.dis+dis[b.u];
}
};
priority_queue<node,vector<node>,cmp >que;
void solve(){
que.push(node(,));
while(!que.empty()){
node U=que.top();que.pop();
while(U.u==n){
tot++;
printf("%d\n",U.dis);
if(tot==K || que.empty())return;
U=que.top();
que.pop();
}
int u=U.u;
for(int i=vd[u],v;i;i=ve[i].nxt){
v=ve[i].v;
que.push(node(v,U.dis+ve[i].w));
}
}
return;
}
int main(){
int i,j,u,v,w;
n=read();m=read();K=read();
for(i=;i<=m;i++){
u=read();v=read();w=read();
(u>v)?add_edge(u,v,w):add_edge(v,u,w);
}
Dij();
solve();
for(i=tot+;i<=K;i++)printf("-1\n");
return ;
}

洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging的更多相关文章

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

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

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

    传送门 题目大意:n头牛在单行道n个位置,开始用不同的速度跑步. 当后面的牛追上前面的牛,后面的牛会和前面的牛以一样的速度 跑,称为一个小团体.问:ts后有多少个小团体. 题解:模拟 倒着扫一遍,因为 ...

  3. 洛谷 3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 题解

    本蒟蒻又来发题解了, 一道较水的模拟题. 题意不过多解释, 思路如下: 在最开始的时候求出每头牛在t秒的位置(最终位置 然后,如果后一头牛追上了前一头牛,那就无视它, 把它们看成一个整体. else ...

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

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

  5. 洛谷P3111 [USACO14DEC]牛慢跑Cow Jog_Sliver 性质分析

    Code: #include<cstdio> #include<algorithm> #include<cstring> using namespace std; ...

  6. 洛谷P3045 [USACO12FEB]牛券Cow Coupons

    P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB ...

  7. 洛谷——P2952 [USACO09OPEN]牛线Cow Line

    P2952 [USACO09OPEN]牛线Cow Line 题目描述 Farmer John's N cows (conveniently numbered 1..N) are forming a l ...

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

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

  9. [洛谷P3014][USACO11FEB]牛线Cow Line (康托展开)(数论)

    如果在阅读本文之前对于康托展开没有了解的同学请戳一下这里:  简陋的博客    百度百科 题目描述 N(1<=N<=20)头牛,编号为1...N,正在与FJ玩一个疯狂的游戏.奶牛会排成一行 ...

随机推荐

  1. c# 调取 c++ dll____c#调用dll

    1.以海康摄像头dll为例.(文章转载https://www.cnblogs.com/smartsensor/p/4343744.html) 海康SDK编程指南 目前使用的海康SDK包括IPC_SDK ...

  2. 福大软工1816:Alpha(6/10)

    Alpha 冲刺 (6/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.组织会议 2.帮助队员解决 ...

  3. android在程序崩溃时Catch异常并处理

    Android系统的"程序异常退出",给应用的用户体验造成不良影响.为了捕获应用运行时异常并给出友好提示,便可继承UncaughtExceptionHandler类来处理.通过Th ...

  4. python学习笔记03:python的核心数据类型

    从根本上讲,Python是一种面向对象的语言.它的类模块支持多态,操作符重载和多重继承等高级概念,并且以Python特有的简洁的语法和类型,OOP十分易于使用.Python的语法简单,容易上手. Py ...

  5. lintcode-33-N皇后问题

    33-N皇后问题 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中" ...

  6. Vim新手节省时间的10多个小技巧

    Vim新手节省时间的10多个小技巧 Vim 是很多开发者的首选编辑器,通过设置正确的命令和快捷方式,它可以帮你更快的完成工作.这篇文章我们为 Vim 新手提供一些快捷键等方面的小技巧,帮你提升工作效率 ...

  7. 使用WCF上传数据

    通过传递Stream对象来传递大数据文件,但是有一些限制: 1.只有 BasicHttpBinding.NetTcpBinding 和 NetNamedPipeBinding 支持传送流数据. 2. ...

  8. Android基础------高级ul:消息对话框

    前言:Android消息对话框提示笔记,刚刚接触Android 1.经典模式 //列表对话框 //经典模式 public void listdialog_01(View view){ final St ...

  9. python 内存分析

    1.改源码重新编译打印相关信息 obmalloc.c 文件中打印 maxarenas,值为当前环境分配 arena 个数:分配 arena 时并没有马上分配对应的pools,故对于每一个 arena, ...

  10. Oracle-RAC原理

    Oracle-RAC原理 来源 https://blog.csdn.net/qq_34556414/article/details/79001267 单点数据库 VS RAC 单节点数据库,如果实例宕 ...