题目描述

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. 用逗号隔开简单数据保存为csv

    用记事本编辑简单数据,用英文逗号隔开,编辑为多列,保存为.csv文件.可以用Excel打开编辑.

  2. ACM 第一天

    标签库元素: 队列<queue> FIFO 栈 <stack>  FICO 集合 set 不定长数组  vector 映射 map Maximum Multiple Time ...

  3. TCP系列38—拥塞控制—1、概述

    在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...

  4. 《学习OpenCV》课后习题解答5

    题目:(P104) 为一个图像创建多个图像头.读取一个大小至少为100*100的图像.另创建两个图像头并设置它们的origion,depth,nChannels和widthStep属性同之前读取的图像 ...

  5. C#中堆和栈的区别?

    http://www.jb51.net/article/55306.htm http://www.cnblogs.com/JimmyZhang/archive/2008/01/31/1059383.h ...

  6. 如何将PDF的背景色设置为保护眼睛的苹果绿色

      福昕阅读器请戳这里.   Adobe Acrobat请戳这里.

  7. CentOS 访问控制列表(tcp wrappers)

    1.TCP Wrappers是一个工作在应用层的安全工具,它只能针对某些具体的应用或者服务起到一定的防护作用.比如说ssh.telnet.FTP等服务的请求,都会先受到TCP Wrappers的拦截. ...

  8. [SCOI2013]摩托车交易 kruskal重构树(最大生成树) 倍增

    ---题面--- 题解: 这题想法简单,,,写起来真的是失智,找了几个小时的错误结果是inf没开到LL范围.... 首先我们需要找到任意两点之间能够携带黄金的上限值,因为是在经过的道路权值中取min, ...

  9. 原 cocos2dx中毒冰冻shader

    #ifdef GL_ES precision mediump float; #endif uniform sampler2D u_texture; varying vec2 v_texCoord; v ...

  10. BZOJ1179 [Apio2009]Atm 【tarjan缩点】

    1179: [Apio2009]Atm Time Limit: 15 Sec  Memory Limit: 162 MB Submit: 4048  Solved: 1762 [Submit][Sta ...