• poj3635Full Tank? 广搜最短路


    Full Tank?
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5754   Accepted: 1891

    Description

    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?

    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 ≤ n ≤ 1000 and 0 ≤ m ≤ 10000, the number of cities and roads. Then follows a line with n integers 1 ≤ pi ≤ 100, where pi is the fuel price in the ith city. Then follow m lines with three integers 0 ≤ uv < n and 1 ≤ d ≤ 100, telling that there is a road between u and v with length d. Then comes a line with the number 1 ≤ q ≤ 100, giving the number of queries, and q lines with three integers 1 ≤ c ≤ 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
    --------------

    【题意】
    给你 n 个点,m 条边,每走 1 单位的路径都会花费 1 单位的 fuel ,并且不同的点灌油的油的价格是不同的,现在给你一些询问,每一个询问给你起点、终点以及油箱的容量,问你所需要的最少的花费可以从起点到达终点。

    ----------

    /** head-file **/
    
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <iomanip>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <list>
    #include <set>
    #include <map>
    #include <algorithm>
    
    /** define-for **/
    
    #define REP(i, n) for (int i=0;i<int(n);++i)
    #define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
    #define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
    #define REP_1(i, n) for (int i=1;i<=int(n);++i)
    #define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
    #define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
    #define REP_N(i, n) for (i=0;i<int(n);++i)
    #define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
    #define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
    #define REP_1_N(i, n) for (i=1;i<=int(n);++i)
    #define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
    #define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
    
    /** define-useful **/
    
    #define clr(x,a) memset(x,a,sizeof(x))
    #define sz(x) int(x.size())
    #define see(x) cerr<<#x<<" "<<x<<endl
    #define se(x) cerr<<" "<<x
    #define pb push_back
    #define mp make_pair
    
    /** test **/
    
    #define Display(A, n, m) {                      
        REP(i, n){                                  
            REP(j, m) cout << A[i][j] << " ";       
            cout << endl;                           
        }                                           
    }
    
    #define Display_1(A, n, m) {                    
        REP_1(i, n){                                
            REP_1(j, m) cout << A[i][j] << " ";     
            cout << endl;                           
        }                                           
    }
    
    using namespace std;
    
    /** typedef **/
    
    typedef long long LL;
    
    /** Add - On **/
    
    const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
    const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
    const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };
    
    const int MOD = 1000000007;
    const int INF = 0x3f3f3f3f;
    const long long INFF = 1LL << 60;
    const double EPS = 1e-9;
    const double OO = 1e15;
    const double PI = acos(-1.0); //M_PI;
    
    const int maxn=2111;
    const int maxm=31111;
    int n,m;
    
    struct EDGENODE{
        int to;
        int w;
        int next;
    };
    struct SGRAPH{
        int head[maxn];
        EDGENODE edges[maxm];
        int edge;
        void init(){
            clr(head,-1);
            edge=0;
        }
        void addedge(int u,int v,int c=0){
            edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
        }
    }dragonborn;
    struct data{
        int u,f,c;
        data(){}
        data(int u,int f,int c){
            this->u=u;
            this->f=f;
            this->c=c;
        }
        friend bool operator<(const data& a,const data& b)
        {
            return a.c>b.c;
        }
    };
    
    int cost[maxn];
    bool vis[maxn][111];
    int solve(SGRAPH& G,int V,int s,int e)
    {
        priority_queue<data>que;
        clr(vis,0);
        que.push(data(s,0,0));
        while (!que.empty())
        {
            data top=que.top();
            que.pop();
            vis[top.u][top.f]=true;
            if (top.u==e) return top.c;
            if (top.f<V&&!vis[top.u][top.f+1])
                que.push(data(top.u,top.f+1,top.c+cost[top.u]));
            for (int i=G.head[top.u];i!=-1;i=G.edges[i].next)
            {
                if (top.f>=G.edges[i].w&&!vis[G.edges[i].to][top.f-G.edges[i].w])
                    que.push(data(G.edges[i].to,top.f-G.edges[i].w,top.c));
            }
        }
        return -1;
    }
    
    int main()
    {
        while (~scanf("%d%d",&n,&m))
        {
            int Q;
            dragonborn.init();
            REP(i,n) scanf("%d",&cost[i]);
            REP(i,m)
            {
                int u,v,d;
                scanf("%d%d%d",&u,&v,&d);
                dragonborn.addedge(u,v,d);
                dragonborn.addedge(v,u,d);
            }
            scanf("%d",&Q);
            while (Q--)
            {
                int c,s,e;
                scanf("%d%d%d",&c,&s,&e);
                int ret=solve(dragonborn,c,s,e);
                if (ret!=-1) printf("%d
    ",ret);
                else printf("impossible
    ");
            }
        }
        return 0;
    }
    


  • 相关阅读:
    C#中的委托,匿名方法和Lambda表达式
    模式化窗口问题![window.dialogArguments]
    js动态改变HiddenField值,后台不能获取值的问题
    将DataTable进行分页并生成新的DataTable
    出错提示为:该行已经属于另一个表 的解决方法
    如何在服务器端获得showModalDialog传递的参数
    Synchronized和SyncRoot与 集合类 的同步
    Oracle中数据出现####的问题
    如何防止多次提交按钮造成重复提交
    Invoke and BeginInvoke
  • 原文地址:https://www.cnblogs.com/cyendra/p/3681607.html
Copyright © 2020-2023  润新知