zoukankan      html  css  js  c++  java
  • uva 11367 最短路

     

      F: Full Tank? 

    After going through the receipts from your car trip through Europe this summer, you realised that the gas prices varied between the cities you visited. Maybe you could have saved some money if you were a bit more clever about where you filled your fuel?

    epsfbox{p11367.eps}

    To help other tourists (and save money yourself next time), you want to write a program for finding the cheapest way to travel between cities, filling your tank on the way. We assume that all cars use one unit of fuel per unit of distance, and start with an empty gas tank.

    Input 

    The first line of input gives 1$ le$n$ le$1000 and 0$ le$m$ le$10000 , the number of cities and roads. Then follows a line with n integers 1$ le$pi$ le$100 , where pi is the fuel price in the i th city. Then follow m lines with three integers 0$ le$u , v < n and 1$ le$d$ le$100 , telling that there is a road between u and v with length d . Then comes a line with the number 1$ le$q$ le$100 , giving the number of queries, and q lines with three integers 1$ le$c$ le$100 , s and e , where c is the fuel capacity of the vehicle, s is the starting city, and e is the goal.

    Output 

    For each query, output the price of the cheapest trip from s to e using a car with the given capacity, or ``impossible" if there is no way of getting from s to e with the given car.

    Sample Input 

    5 5 
    10 10 20 12 13 
    0 1 9 
    0 2 8 
    1 2 1 
    1 3 11 
    2 3 7 
    2 
    10 0 3 
    20 1 4
    

    Sample Output 

    170 
    impossible
    

    建模:(参考:https://gist.github.com/andmej/1360763      && uva 论坛)

    点为d[i][j],表示到第i个城市当前有j units油的状态;边表示花费,对d[i][j]按一下方式引出弧:

    1.若k为i的直接后继,且j>=dis(i, k),则引弧d[i][j]到d[k][j-dis(i,k)],费用0;

    2.若当前车还可加油,即j<c,则引弧d[i][j]到d[i][j+1]。

    另外,这种弧是在dijkstra时建立的。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<stack>
    
    using namespace std;
    
    #define LL long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define cint const int
    #define INF 100000000
    #define MAXN 1111
    #define MAXM 22222
    
    struct edge{
        int u, v, w, nxt;
    }e[MAXM];
    int h[MAXN], cc;
    int n, m, p[MAXN], c;
    
    void add(int u, int v, int w){
        e[cc]=(edge){u, v, w, h[u]};
        h[u]=cc++;
        e[cc]=(edge){v, u, w, h[v]};
        h[v]=cc++;
    }
    
    struct node{
        int u, cost, g;
        bool operator < (const node &rhs)const{
            return cost > rhs.cost;
        }
    };
    
    int d[1111][111];
    int dijkstra(cint s, cint t){
        for(int i=0; i<n; i++) fill_n(d[i], c+1, INF);
        priority_queue<node> q;
        d[s][0]=0;     q.push((node){s, 0, 0});
        while(!q.empty()){
            node ut = q.top();  q.pop();
            int u=ut.u;
            if(d[u][ut.g]<ut.cost) continue;
            if(u == t) return d[u][ut.g];
    
            if(ut.g<c && d[u][ut.g+1]>ut.cost+p[u]){
                d[u][ut.g+1] = ut.cost + p[u];
                q.push((node){u, d[u][ut.g+1], ut.g+1});
            }
    
            for(int i=h[u]; i!=-1; i=e[i].nxt){
                int v = e[i].v , dis = e[i].w, new_g = ut.g-dis;
                if(new_g>-1 && d[v][new_g]>d[u][ut.g]){
                    d[v][new_g] = d[u][ut.g];
                    q.push((node){v, d[v][new_g], new_g});
                }
            }
        }
        return INF;
    }
    
    int main(){
    //    freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d %d", &n, &m)==2){
            int i, u, v, w;
            for(i=0; i<n; i++) scanf(" %d", p+i);
            fill_n(h, n, -1);       cc=0;
            for(i=0; i<m; i++){
                scanf(" %d %d %d", &u, &v, &w);
                add(u, v, w);
            }
            int q, s, e;
            scanf(" %d", &q);
            while(q--){
                scanf(" %d %d %d", &c, &s, &e);
                int ans = dijkstra(s, e);
                if(ans == INF) printf("impossible
    ");
                else printf("%d
    ", ans);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    关于嵌入式的技术竞争力
    CentOS7使用ISO镜像文件作为离线Yum源
    CentOS7搭建NAS文件共享存储
    CentOS7安装redis5.0.6
    Windows 10 2004及以后版本删除更新历史记录
    CentOS7手动安装MySQL 8
    python 使用lambda对dict排序
    单片机驱动-软件模拟I2C
    Linux-通过网络nfs和tftp在开发板上运行linux驱动程序
    体验一个前端视图层的mvvm的框架Knockoutjs(双向绑定,模板..)..解放您的双手,不再些那么多的dom操作..快速实现视图层数据与UI的交互处理
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3375333.html
Copyright © 2011-2022 走看看