zoukankan      html  css  js  c++  java
  • POJ 3635 Full Tank?

    Full Tank?
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5619   Accepted: 1836

    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

    Source

     
     

    题意:有 n 个城市  ,每个 城市的 油价 不一样 ,已知 各 城市之间的距离 ,(假设 一  单位 距离 耗费一单位的 油量)一辆车 的  油箱 最多可以 装 c 升油,求从 s  到 e,的 最小 花费油量,如果不能到达 输出 impossible 。

    题解:一开始 自己 写了 个,到达每一点后枚举 可以 增加的 油量 结果 tle (太多 无用的状态了)。。。。。。

    dp[i][j]   表示 到达 城市  i  剩余油量 为 j  的 最小花费; 

    首先到达每个城市的后 要加的 油量 是 不确定 的 ,所以 我们要 将  每一个城市 拆成 c+ 1 个点,(不是直接的拆分),在 每一个 状态节点  我们有 两种选择 ,1  : 油量  加 1  (这个 就 相当于 拆分 成了  c+1 个节点);2:不加油 直接 走向  可 扩展的节点 。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    const int VM=1010;
    const int EM=10010;
    #define INF 100000000000000
    
    struct Edge{
        int to,nxt;
        int cap;
    }edge[EM<<1];
    
    struct Qnode{
        int u,d;
        int cost;
        Qnode(int _u,int _d,int _cost):u(_u),d(_d),cost(_cost){}
        bool operator < (const Qnode &a) const{
            return a.cost<cost;
        }
    };
    
    int n,m,cnt,money[VM];
    int head[VM],dp[VM][110],vis[VM][110];
    
    void addedge(int cu,int cv,int cw){
        edge[cnt].to=cv;
        edge[cnt].cap=cw;
        edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
    }
    
    int BFS(int src,int des,int C){
        priority_queue<Qnode> q;
        while(!q.empty())
            q.pop();
        for(int i=0;i<=n;i++)
            for(int j=0;j<=C;j++)
                dp[i][j]=INF;
        memset(vis,0,sizeof(vis));
        dp[src][0]=0;
        q.push(Qnode(src,0,0));
        while(!q.empty()){
            Qnode cur=q.top();
            q.pop();
            vis[cur.u][cur.d]=1;    // 标记  已经找到了 的 最小的  ,下面就不用 再扩展的 此节点了
            if(cur.u==des)  // 用的是 优先队列 所以 出来的定是 最小的 ;
                return cur.cost;
            if(cur.d+1<=C && !vis[cur.u][cur.d+1] && dp[cur.u][cur.d+1]>dp[cur.u][cur.d]+money[cur.u]){ //油量 加 1
                dp[cur.u][cur.d+1]=dp[cur.u][cur.d]+money[cur.u];
                q.push(Qnode(cur.u,cur.d+1,dp[cur.u][cur.d+1]));
            }
            for(int i=head[cur.u];i!=-1;i=edge[i].nxt){ // 直接 走向 相邻 节点 ;
                int v=edge[i].to;
                int cap=edge[i].cap;
                if(cur.d>=cap && !vis[v][cur.d-cap] && dp[v][cur.d-cap]>cur.cost){
                    dp[v][cur.d-cap]=cur.cost;
                    q.push(Qnode(v,cur.d-cap,cur.cost));
                }
            }
        }
        return -1;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d",&n,&m)){
            cnt=0;
            memset(head,-1,sizeof(head));
            for(int i=0;i<n;i++)
                scanf("%d",&money[i]);
            int u,v,w;
            while(m--){
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
                addedge(v,u,w);
            }
            int q;
            scanf("%d",&q);
            while(q--){
                scanf("%d%d%d",&w,&u,&v);
                int ans=BFS(u,v,w);
                if(ans!=-1)
                    printf("%d\n",ans);
                else
                    printf("impossible\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    响应式卡片抽奖插件 CardShow
    Day10-微信小程序实战-交友小程序-自定义callPhone 和copyText组件
    Day10-微信小程序实战-交友小程序-对点赞次数的优化及首页推荐与最新功能实现-详情页渲染数据和样式布局
    Day8-微信小程序实战-交友小程序-首页用户列表渲染及多账号调试及其点赞功能的实现
    Day8-微信小程序实战-交友小程序-头像的设置
    Day8-微信小程序实战-交友小程序-创建编辑子页和修改个人信息页面
    Day7-微笑小程序实战-交友小程序-登陆模块
    Day7-微笑小程序-交友小程序之“我的”页面-创立用户数据库
    Day7-微信小程序实战-交友小程序首页UI
    Day7-微信小程序实战-交友小程序-tabbar实现
  • 原文地址:https://www.cnblogs.com/jackge/p/3072548.html
Copyright © 2011-2022 走看看