zoukankan      html  css  js  c++  java
  • #图# #dijkstra# ----- OpenJudge 726:ROADS

    OpenJudge 726:ROADS

    总时间限制: 1000ms内存限制: 65536kB

    描述
    N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins). 
    Bob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash. 

    We want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has. 
    输入
    The first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way. 
    The second line contains the integer N, 2 <= N <= 100, the total number of cities. 

    The third line contains the integer R, 1 <= R <= 10000, the total number of roads. 

    Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters : 
    • S is the source city, 1 <= S <= N 
    • D is the destination city, 1 <= D <= N 
    • L is the road length, 1 <= L <= 100 
    • T is the toll (expressed in the number of coins), 0 <= T <=100

    Notice that different roads may have the same source and destination cities.
    输出
    The first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. 
    If such path does not exist, only number -1 should be written to the output. 
    样例输入
    5
    6
    7
    1 2 2 3
    2 4 3 3
    3 4 2 4
    1 3 4 1
    4 6 2 1
    3 5 2 0
    5 4 3 2
    
    样例输出
    11

    求单源最短路。优先队列排序,路长为第一关键字,消耗费用为第二关键字。

     1 #include<cstdio>
     2 #include<queue>
     3 using namespace std;
     4 int k,n,R;
     5 int head[105];
     6 struct node{
     7     int v;
     8     int l,w;
     9     int d;
    10     int next;
    11     bool operator < (const node & a) const{//重载运算符,多关键字排序 
    12         if(d==a.d)return w>a.w;
    13         else return d>a.d;
    14     }
    15 }edge[10005];
    16 
    17 priority_queue<node>Q;
    18 
    19 int dijkstra(){
    20     node now1;
    21     now1.v=1;
    22     now1.w=0;
    23     now1.d=0;
    24     Q.push(now1);
    25     while(!Q.empty()){
    26         node now=Q.top();
    27         if(now.v==n)return now.d;
    28         Q.pop();
    29         for(int i=head[now.v];i;i=edge[i].next)
    30             if(k>=now.w+edge[i].w){
    31                 node now2;
    32                 now2.v=edge[i].v;
    33                 now2.w=now.w+edge[i].w;
    34                 now2.d=now.d+edge[i].l;
    35                 Q.push(now2);
    36             }
    37     }
    38 }
    39 
    40 int main(){
    41     scanf("%d%d%d",&k,&n,&R);
    42     for(int i=1;i<=R;++i){
    43         int x;
    44         scanf("%d%d%d%d",&x,&edge[i].v,&edge[i].l,&edge[i].w);
    45         edge[i].next=head[x];
    46         head[x]=i;
    47     }
    48     printf("%d
    ",dijkstra());
    49     return 0;
    50 }
     
  • 相关阅读:
    iOS 各种编译错误汇总
    Reveal查看任意app的高级技巧
    PCH in Xcode 6
    iOS开发之工具篇-20个可以帮你简化移动app开发流程的工具
    UICollectionViewController xcode6.1 自定义Cell
    Xcode6.1 Prefix.pch添加方式
    最近开始研究php的缓存技术,来个系统自带的OPcache
    今天练手了下mysqlbinlog,标记下
    写了个数组多个数组合并返回的是不重复的数组
    ngnix配置thinkphp5隐藏index.php的方法亲测有效
  • 原文地址:https://www.cnblogs.com/wjting/p/6025821.html
Copyright © 2011-2022 走看看