zoukankan      html  css  js  c++  java
  • (BFS) poj 3635

    Full Tank?
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6724   Accepted: 2194

    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

    题意 给出一些点,这些点出都有汽油的价钱,并且每条边都有一定花费

    dp[u][s]表示 到 u点时,还有 s个油的最小花费然后进行转移。。。。注意要用优先队列

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<set>
    using namespace std;
    vector<int> e[1010],w[1010];
    int n,m,pri[1010],c,ans,s,t;
    int dp[1010][105],vis[1010][105];
    struct node
    {
        int x,cost,fe;
        bool friend operator<(node a,node b)
        {
            return a.cost>b.cost;
        }
    };
    bool BFS()
    {
        memset(dp,0x3f,sizeof(dp));
        memset(vis,0,sizeof(vis));
        priority_queue<node> q;
        node fr,st;
        fr.x=s,fr.cost=0,fr.fe=0;
        q.push(fr);
        dp[s][0]=0;
        while(!q.empty())
        {
            fr=q.top();
            q.pop();
            int u=fr.x,cost=fr.cost,fe=fr.fe;
            if(u==t)
            {
                ans=cost;
                return true;
            }
            vis[u][fe]=1;
            if(fe+1<=c&&!vis[u][fe+1]&&dp[u][fe]+pri[u]<dp[u][fe+1])
            {
                dp[u][fe+1]=dp[u][fe]+pri[u];
                st.x=u,st.cost=dp[u][fe+1],st.fe=fe+1;
                q.push(st);
            }
            for(int i=0;i<e[u].size();i++)
            {
                int v=e[u][i];
                int ww=w[u][i];
                if(fe<ww)
                    continue;
                if(!vis[v][fe-ww]&&cost<dp[v][fe-ww])
                {
                    dp[v][fe-ww]=cost;
                    st.x=v,st.cost=cost,st.fe=fe-ww;
                    q.push(st);
                }
            }
        }
        return false;
    }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&pri[i]);
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            x++,y++;
            e[x].push_back(y);
            e[y].push_back(x);
            w[x].push_back(z);
            w[y].push_back(z);
        }
        int qq;
        scanf("%d",&qq);
        while(qq--)
        {
            scanf("%d%d%d",&c,&s,&t);
            s++,t++;
            if(BFS())
                printf("%d
    ",ans);
            else
                printf("impossible
    ");
        }
        return 0;
    }
    

      

  • 相关阅读:
    子程序的设计
    多重循环程序设计
    汇编语言的分支程序设计与循环程序设计
    代码调试之串口调试2
    毕昇杯模块之光照强度传感器
    毕昇杯之温湿度采集模块
    【CSS】盒子模型 之 IE 与W3C的盒子模型对比
    【css】盒子模型 之 概述
    【css】盒子模型 之 弹性盒模型
    【网络】dns_probe_finished_nxdomain 错误
  • 原文地址:https://www.cnblogs.com/water-full/p/4527920.html
Copyright © 2011-2022 走看看