zoukankan      html  css  js  c++  java
  • 【图论补完计划】poj 3635 (最短路变形)

    Full Tank?
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7427   Accepted: 2399

    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 ≤ u, v < 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

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn=1005;
    const int maxf=105;
    const int inf=1e9;
    
    typedef pair<int,int> P;
    
    struct node{
        int f,v,t;
        bool operator <(const node&a)const{
            return a.v<v;
        }
    };
    
    int dp[maxn][maxf];
    bool used[maxn][maxf];
    int p[maxn];
    
    int cap,st,ed;
    
    vector<P> G[maxn];
    
    void init(){
        for(int i=0;i<maxn;i++){
            for(int j=0;j<maxf;j++){
                dp[i][j]=inf;
            }
        }
        dp[st][0]=0;
        memset(used,0,sizeof(used));
    }
    
    int dij(){
        priority_queue<node> q;
        q.push((node){0,0,st});
        node tem;
        while(!q.empty()){
            node now=q.top();
            q.pop();
            int tf=now.f;
            int tv=now.v;
            int tt=now.t;
            used[tt][tf]=1;
            if(tt==ed) return tv;
            if(tf+1<=cap&&!used[tt][tf+1]&&dp[tt][tf+1]>dp[tt][tf]+p[tt]){
                dp[tt][tf+1]=dp[tt][tf]+p[tt];
                tem=(node){tf+1,dp[tt][tf+1],tt};
                q.push(tem);
            }
            for(int i=0;i<G[tt].size();i++){
                int v=G[tt][i].first;
                int w=G[tt][i].second;
                if(tf-w>=0&&!used[v][tf-w]&&dp[v][tf-w]>tv){
                    dp[v][tf-w]=tv;
                    tem=(node){tf-w,dp[v][tf-w],v};
                    q.push(tem);
                }
            }
        }
        return -1;
    }
    
    int main(){
        int n,m;
        scanf("%d %d",&n,&m);
        for(int i=0;i<n;i++){
            scanf("%d",&p[i]);
        }
        for(int i=0;i<m;i++){
            int s,t,v;
            scanf("%d %d %d",&s,&t,&v);
            G[s].push_back(P(t,v));
            G[t].push_back(P(s,v));
        }
        int T;
        scanf("%d",&T);
        while(T--){
            scanf("%d %d %d",&cap,&st,&ed);
            init();
            int res=dij();
            if(res!=-1) printf("%d
    ",res);
            else printf("impossible
    ");
        }
        return 0;
    }
  • 相关阅读:
    卸载全部appx应用(包括应用商店)
    重置 Launchpad 和更新APP图标缓存
    删除OSX中第三方的「偏好设置」程序(.prefPane)
    IOS segue(跳转页面处理)
    IOS NSNotificationCenter(通知 的使用)监听文本框的文字改变
    IOS UITextFieldDelegate (常用的代理方法)
    IOS UIActionSheet(底部 弹出框的使用)
    IOS 偏好设置数据 存 取(Preferences文件夹)
    IOS plist的数据 存 取(沙河目录)
    显示 Mac隐藏的文件夹 命令语句
  • 原文地址:https://www.cnblogs.com/hymscott/p/6496095.html
Copyright © 2011-2022 走看看