• [POJ] 1724 ROADS


    ROADS

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9971   Accepted: 3706

    Description

    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. 

    Input

    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.

    Output

    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. 

    Sample Input

    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
    

    Sample Output

    11

    Source

     
     
     
    题解:该图所有路都为单向路,每条路有两个限制元素,路长length和通行费toll,求总花费在不超过K的情况下从点1到点n的最短路。根据数据类型和规模选用spfa即可,松弛过程中再运用动态规划的思想,维护一个dist[vi][ki],代表在节点vi花费ki时走的最短路。预处理dist[i][j]=MAX_NUMBER,dist[1][0]=0;
     
    代码:
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<limits.h>
      4 #include<stdbool.h>
      5 int i,j,n,m,ki,r,x,y,z,p,tot,length,
      6     toit[110000],list[110],next[110000],cost[110000],
      7     q[500000],len[110000],dist[110][11000];
      8 
      9 bool can[110];
     10 int 
     11 pre()
     12 {
     13     memset(toit,0,sizeof(toit));
     14     memset(list,0,sizeof(list));
     15     memset(next,0,sizeof(next));
     16     memset(dist,3,sizeof(dist));
     17     memset(cost,0,sizeof(cost));
     18     memset(can,true,sizeof(can));
     19     memset(len,0,sizeof(len));
     20     tot=0;length=35111111;
     21     return 0;
     22 }
     23 
     24 int 
     25 min(int a,int b)
     26 {
     27     if(a<b) return(a);
     28     else return(b);
     29 }
     30 
     31 int 
     32 add(int x,int y,int z,int p)
     33 {
     34     tot++;
     35     toit[tot]=y;
     36     next[tot]=list[x];
     37     list[x]=tot;
     38     cost[tot]=p;
     39     len[tot]=z;
     40     
     41     return 0;
     42 }
     43 
     44 int 
     45 spfa(int s)
     46 {
     47     int i,j,head,tail,v,k;
     48     head=0;tail=1;
     49     q[1]=s; 
     50     can[s]=false;
     51     
     52     while(head!=tail)
     53     {
     54         head=head%500000+1;
     55         v=q[head];
     56         k=list[v];
     57         
     58         while(k!=0)
     59         {                   
     60             for(i=cost[k];i<=ki;i++)
     61             if(dist[v][i-cost[k]]+len[k]<dist[toit[k]][i])
     62             {
     63                 dist[toit[k]][i]=dist[v][i-cost[k]]+len[k];
     64                 if(can[toit[k]])
     65                 {
     66                     tail=tail%500000+1;
     67                     q[tail]=toit[k];
     68                     can[toit[k]]=false;
     69                 }
     70             }
     71             k=next[k];
     72         }
     73         can[v]=true;
     74     }
     75     return 0;
     76 }
     77 
     78 int 
     79 init()
     80 {
     81     scanf("%d
    %d
    %d
    ",&ki,&n,&r);
     82     for(i=1;i<=r;i++)
     83     {
     84         scanf("%d%d%d%d",&x,&y,&z,&p);
     85         add(x,y,z,p);
     86     }
     87     return 0;
     88 }
     89 
     90 int 
     91 main()
     92 {
     93     pre();
     94     init();
     95     dist[1][0]=0;
     96     spfa(1);
     97     
     98     for(i=0;i<=ki;i++)
     99     length=min(length,dist[n][i]);
    100     
    101     if(length==35111111) printf("-1
    ");else
    102     printf("%d
    ",length);
    103     return 0;
    104 }
    105     
     
     
  • 相关阅读:
    nasm astrstr函数 x86
    nasm astrspn函数 x86
    nasm astrset_s函数 x86
    nasm astrrev函数 x86
    模板:最长公共子序列(LCS)
    模板:最小生成树(MST)
    模板:并查集
    模板:单源最短路径
    模板:最近公共祖先(LCA)
    模板:线段树(1)——加法,求和
  • 原文地址:https://www.cnblogs.com/sxiszero/p/3617844.html
Copyright © 2020-2023  润新知