zoukankan      html  css  js  c++  java
  • poj3635 优先队列+打标记+广搜

    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
    题意:给出一张图,n<=1000,m<=10000.  有一辆车想从图的一个地方到达另外一个地方,每个点是一个卖油的地方,每个地方买的有价格不一样,车的最大装油量是c,求初始点到终止点的最小花费。

    因为状态数只有n*100=10W 所以放心的爆搜就好了 ,,自己太蠢 爆搜都不敢写‘’
    #include<cstdio>
    #include<cstring>
    #include<queue>
    const int N=1010;
    using namespace std;
    struct Edge{
        int v,w,next;
    }e[20010];
    int tot,head[N];
    void add(int u,int v,int w){
       e[tot].w=w;
       e[tot].v=v;
       e[tot].next=head[u];
       head[u]=tot++;
    }
    int n,m,p[N],x,y,z,dp[N][110],c,s,t;
    bool vis[N][110];
    struct Node{
        int u,cost,oil;
        Node(){}
        Node(int a,int b,int c):u(a),cost(b),oil(c){}
        bool operator<(const Node &A)const{
        return cost>A.cost;
        }
    };
    void bfs(){
       memset(dp,0x3f,sizeof(dp));
       memset(vis,0,sizeof(vis));
       dp[s][0]=0;
       priority_queue<Node>Q;
       Q.push(Node(s,0,0));
       while(!Q.empty()){
        Node now=Q.top();Q.pop();
        int o=now.oil,u=now.u,cost=now.cost;
        vis[u][o]=1;
        if(u==t) {
            printf("%d
    ",cost);
            return ;
        }
        if(o+1<=c&&!vis[u][o+1]&&dp[u][o]+p[u]<dp[u][o+1]) {
            dp[u][o+1]=dp[u][o]+p[u];
            Q.push(Node(u,dp[u][o+1],o+1));
        }
        for(int i=head[u];i+1;i=e[i].next){
            int v=e[i].v,w=e[i].w;
            if(o>=w&&!vis[v][o-w]&&cost<dp[v][o-w]){
                dp[v][o-w]=cost;
                Q.push(Node(v,dp[v][o-w],o-w));
            }
        }
       }
       puts("impossible");
    }
    int main(){
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i) scanf("%d",p+i);
        for(int i=1;i<=m;++i) {
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        int T;
        for(scanf("%d",&T);T--;){
            scanf("%d%d%d",&c,&s,&t);
            bfs();
        }
    }
  • 相关阅读:
    算法(一)—— 河内之塔(汉诺塔)
    JAVA爬取网页邮箱
    js中判断某字符串含有某字符出现的次数
    逻辑删除和物理删除的区别
    Forward和Redirect的区别
    Postman 传Map类型的参数
    Java基础
    【html-css】
    【HTML----】
    【python-while-以及字符串的相关操作和函数】
  • 原文地址:https://www.cnblogs.com/mfys/p/7241592.html
Copyright © 2011-2022 走看看