zoukankan      html  css  js  c++  java
  • 洛谷P2901 [USACO08MAR]牛慢跑Cow Jogging

    题目描述

    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* 最短路

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

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 #include<queue>
     7 using namespace std;
     8 const int inf=0x3f3f3f3f;
     9 const int mxn=20010;
    10 int read(){
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 struct edge{
    17     int v,nxt,w;
    18 }e[mxn<<1],ve[mxn<<1];
    19 int hd[mxn],mct=0;
    20 int vd[mxn],vct=0;
    21 void add_edge(int u,int v,int w){
    22     e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=w;hd[u]=mct;
    23     ve[++vct].v=u;ve[vct].nxt=vd[v];ve[vct].w=w;vd[v]=vct;
    24     return;
    25 }
    26 struct node{
    27     int u,dis;
    28     node(int a,int b){u=a;dis=b;}
    29     friend bool operator < (const node &a,const node &b){
    30         return a.dis>b.dis;
    31     }
    32 };
    33 priority_queue<node>q;
    34 int dis[mxn];
    35 int n,m,K;
    36 void Dij(){
    37     memset(dis,0x3f,sizeof dis);
    38     dis[n]=0;q.push(node(n,0));
    39     while(!q.empty()){
    40         node U=q.top();q.pop();
    41         while(U.dis>dis[U.u]){if(q.empty())break;U=q.top();q.pop();}
    42         int u=U.u;
    43 //        printf("u:%d
    ",u);
    44         for(int i=hd[u];i;i=e[i].nxt){
    45             int v=e[i].v;
    46             if(dis[v]>dis[u]+e[i].w){
    47                 dis[v]=dis[u]+e[i].w;
    48                 q.push((node){v,dis[v]});
    49             }
    50         }
    51     }
    52     return;
    53 }
    54 int tot=0;
    55 struct cmp{
    56     bool operator () (const node a,const node b){
    57         return a.dis+dis[a.u]>b.dis+dis[b.u];
    58     }
    59 };
    60 priority_queue<node,vector<node>,cmp >que;
    61 void solve(){
    62     que.push(node(1,0));
    63     while(!que.empty()){
    64         node U=que.top();que.pop();
    65         while(U.u==n){
    66             tot++;
    67             printf("%d
    ",U.dis);
    68             if(tot==K || que.empty())return;
    69             U=que.top();
    70             que.pop();
    71         }
    72         int u=U.u;
    73         for(int i=vd[u],v;i;i=ve[i].nxt){
    74             v=ve[i].v;
    75             que.push(node(v,U.dis+ve[i].w));
    76         }
    77     }
    78     return;
    79 }
    80 int main(){
    81     int i,j,u,v,w;
    82     n=read();m=read();K=read();
    83     for(i=1;i<=m;i++){
    84         u=read();v=read();w=read();
    85         (u>v)?add_edge(u,v,w):add_edge(v,u,w);
    86     }
    87     Dij();
    88     solve();
    89     for(i=tot+1;i<=K;i++)printf("-1
    ");
    90     return 0;
    91 }
  • 相关阅读:
    PowerDesigner与Eclipse同步开发
    postdrop: create file maildrop/xxx: Permission denied
    mysql导出数据mysqldump用法
    mysql 存储过程中 limit之后使用分页变量,传入分页参数. 类似于微博游标分批次获取信息;问号参数类似于c#的string.format;问号占位符
    mysql 获取本周一的日期,本周日的日期
    mysql 游标 ,嵌套游标
    asp.net调用mysql 存储过程 带 out 返回值,返回刚插入数据库中的自增的ID,LAST_INSERT_ID() 的使用
    跨服务器,跨数据库,多表联合查询 / 如何用sql语句来查询表中哪些记录是重复的
    asp.net 使用mysql数据库,OUT parameter返回值为null的bug
    mysql 类型转换 cast 将 float 转换为 decimal
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6899999.html
Copyright © 2011-2022 走看看