hdu 1676 Full Tank? 限制最短路 dp 蛮有技巧的~

http://acm.hdu.edu.cn/showproblem.php?pid=1676

题意:有n个城市和m条路,一辆车要从s城市到e城市,而且这辆车能够乘的油的容量为cap,每走1单位距离就耗费1单位油。每个城市都有加油站,但是每个加油站的价格不同。让你判断这辆车能否到达城市e,如果能够达到,那么所要耗费的最少价格是多少。

 

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<algorithm>
#include<vector>
#define maxx 9999999
using namespace std;
int d[1002][1002];
int unit[1002];
bool vis[1002][1002];

struct node1{int v,w;};
vector<node1 > g[1002];


struct node
{
    friend bool operator< (node n1, node n2)
    {
        return n1.sum > n2.sum;
    }
    int x;
    int y;
    int sum;
};
priority_queue <node> q;

void solve(int c,int s,int t,int n)
{
    while(!q.empty())
    {
        q.pop();
    }
    node temp;
    memset(d,-1,sizeof(d));
    memset(vis,false,sizeof(vis));

    temp.x=s;  temp.y=0;   temp.sum=0;
    q.push(temp);   //printf("chushi   %d %d %d\n",s,i,d[s][i]);

    while(!q.empty())
    {
        temp=q.top(); q.pop();
        int u,res;
        u=temp.x;   res= temp.y;
        if(u==t)
        {
            printf("%d\n",temp.sum);
            return ;
        }

        if(res<c&&(d[u][res+1]==-1||d[u][res+1]>temp.sum+unit[u]))
           {
               d[u][res+1]=temp.sum+unit[u];
               node temp1;
               temp1.x=u;   temp1.y=res+1;   temp1.sum=d[u][res+1];
               q.push(temp1);
           }
        for(int i=0;i<g[u].size();i++)
        {
            int v,dis;
            v=g[u][i].v;
            dis=g[u][i].w;

            if(res<dis)
                continue;
            if(d[v][res-dis]==-1||d[v][res-dis]>temp.sum)
            {
                 d[v][res-dis]=temp.sum;
                 node temp1; temp1.x=v;  temp1.y= res-dis;  temp1.sum=temp.sum;
                 q.push(temp1);
            }

        }
    }
    printf("impossible\n");
}

int main()
{
    int n,c,i,j,s,t,m;
    scanf("%d%d",&n,&m);
    {
        for(i=0; i<n; i++)
            scanf("%d",&unit[i]);
        for(i=0;i<=n;i++)
            g[i].clear();
        while(m--)
        {
            scanf("%d%d%d",&i,&j,&t);
            node1 temp;
            temp.v=j;
            temp.w=t;
            g[i].push_back(temp);
            temp.v=i;
            g[j].push_back(temp);
        }
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&c,&s,&t);
            solve(c,s,t,n);
        }
    }
    return 0;
}

 

 

 

posted @ 2014-05-06 15:10  galaxy77  阅读(220)  评论(0编辑  收藏  举报