• ROADS


    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 input begins with the number t of test cases. Then t test cases follow. The first line of the each test case 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

    For each test case, output a single line 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, output -1.

    Example

    Input:
    2
    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
    0
    4
    4
    1 4 5 2
    1 2 1 0
    2 3 1 1
    3 4 1 0
    
    Output:
    11
    -1

    题目大意:有n个点 m条边 每条边有长度和花费两个权值。要求在花费《=k的情况下,找出从点1到点n的最短的距离。不存在输出-1

    bfs+优先队列。优先队列只是优化。

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/9/4 19:25:30
    File Name     :spoj_roads.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    
    int n,m,k;
    struct Edge{
        int next,y,l,c;
    }edge[maxn];
    int pre[maxn],L;
    void add(int u,int y,int l,int c){
        edge[L].y=y;
        edge[L].l=l;
        edge[L].c=c;
        edge[L].next=pre[u];
        pre[u]=L++;
    }
    
    struct node{
        int y,l,c;
    };
    struct cmp{
        bool operator()(node a,node b){
            if(a.l==b.l) return a.c>b.c;
            return a.l>b.l;
        }
    };
    
    int bfs(){
        priority_queue<node,vector<node>,cmp>q;
        q.push({1,0,0});
        while(!q.empty()){
            node u=q.top();q.pop();
            if(u.y==n){
                return u.l;
            }
            node tmp;
            //cout<<"y "<<u.y<<endl;
            for(int i=pre[u.y];i+1;i=edge[i].next){
                int y=edge[i].y;
                int c=edge[i].c;
                int l=edge[i].l;
                if(u.c+c<=k){
                    tmp.y=y;
                    tmp.l=u.l+l;
                    tmp.c=u.c+c;
                    q.push(tmp);
                }
            }
        }
        return INF;
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int t,x,y,z,w;
        cin>>t;
        while(t--){
            cin>>k>>n>>m;
            L=0;
            memset(pre,-1,sizeof pre);
            for(int i=1;i<=m;i++){
                scanf("%d %d %d %d",&x,&y,&z,&w);
                add(x,y,z,w);
            }
            int w=bfs();
            if(w==INF){
                puts("-1");
            }
            else cout<<w<<endl;
        }
        return 0;
    }
  • 相关阅读:
    ZOJ 1001 A + B Problem
    献给那些心软的人!!
    将表格的数据插入另一个表格
    把链接 显示为方框
    【ibus】设置ibus输入法(pinyin & sunpinyin)
    [Mongo] How to Install Mongo on Debian(不要安装)
    [Sinatra、Mongo] Mongo
    Sinatra+SQLite3+DataMapper
    [sinatra] Sinatra再入门
    [slim] Slim
  • 原文地址:https://www.cnblogs.com/pk28/p/5840945.html
Copyright © 2020-2023  润新知