夏夜、

心若平似镜、何题不AC。

UVA 11367 Full Tank? 最短路

以状态(u,fuel)为结点建图(把每个点拆成100个),表示在点u时还剩下fuel个单位的燃料,那么状态就可以这样转移:

(u,fuel)->(u,fuel+i)  : 加i单位的燃料,所以这条边的权值就是 i*p[u];

(u,fuel)   ->(v,fuel-dist):  走到另一个点,其中dist为路径。

当然直接这样做可能会超时,需要做两个小优化(做其中一个就能AC了)

1. (u,fuel)->(u,fuel+i) 中的i只需为1就行了,这样就能边数从100^2降到100. 比如(u,fuel)->(u,fuel+3)可以用

  (u,fuel)->(u,fuel+1)->(u,fuel+2)->(u,fuel+3) 代替

2. 没必要计算全部结点,只要确定了d[u][fuel]的值 就break;

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("in.txt","r",stdin);
    //freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

struct HeapNode
{
    int d,u,fuel;
    bool operator <(const HeapNode &ant) const
    {
        return ant.d<d;
    }
};

struct Edge
{
    int u,v,w;
};

const int maxn=1005;
const int maxc=105;
vector<int> g[maxn];
vector<Edge> edge;
int p[maxn];
int n;

void init()
{
    for(int i=0;i<n;i++)
        g[i].clear();
    edge.clear();
}

void add(int u,int v,int w)
{
    Edge e=(Edge){u,v,w};
    edge.push_back(e);
    g[u].push_back(edge.size()-1);
}

bool done[maxn][maxc];
int d[maxn][maxc];

void solve(int s,int e,int c)
{
    memset(done,0,sizeof(done));
    memset(d,INF,sizeof(d));
    d[s][0]=0;
    priority_queue<HeapNode> q;
    q.push((HeapNode){0,s,0});

    while(!q.empty())
    {
        HeapNode x=q.top();q.pop();
        if(done[x.u][x.fuel]) continue;
        int u=x.u,fuel=x.fuel;
        done[u][fuel]=1;
        if(u==e&&fuel==0)break;
        if(fuel!=c&&d[u][fuel]+p[u]<d[u][fuel+1])
        {
            d[u][fuel+1]=d[u][fuel]+p[u];
            q.push((HeapNode){d[u][fuel+1],u,fuel+1});
        }

        for(int i=0;i<g[u].size();i++)
        {
            int v=edge[g[u][i]].v;
            int w=edge[g[u][i]].w;
            if(fuel>=w&&d[u][fuel]<d[v][fuel-w])
            {
                d[v][fuel-w]=d[u][fuel];
                q.push((HeapNode){d[v][fuel-w],v,fuel-w});
            }
        }
    }
    if(d[e][0]!=INF)
        printf("%d\n",d[e][0]);
    else printf("impossible\n");
}
int main()
{
    int m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        for(int i=0;i<n;i++) scanf("%d",&p[i]);
        for(int i=0;i<m;i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            add(u,v,w); add(v,u,w);
        }
        int q;
        scanf("%d",&q);
        for(int i=0;i<q;i++)
        {
            int s,e,c; scanf("%d%d%d",&c,&s,&e);
            solve(s,e,c);
        }
    }
    return 0;
}
View Code

 

posted on 2014-04-05 19:31  BMan、  阅读(200)  评论(0编辑  收藏  举报

导航