UVA 11367 - Full Tank

49 篇文章 0 订阅

/*

最短路问题  可以分解为2种走法  1:原地加一单位油,2:到下一个地点。

*/

#include<cstdio>

#include<cstring>
#include<queue>
#include<vector>
#define M 10010
using namespace std;


struct Edge
{
    int from,to,dist;
    Edge(int x,int y,int z)
    {
        from = x;
        to = y;
        dist = z;
    }
};
struct P
{
    int cost;
    int u,f;
    bool operator < (const P &a) const
    {
        return a.cost < cost;
    }
};
int n,m,s,t,c;
const int INF = 1<<30;
vector<Edge> edges;
vector<int> G[M];
int dp[1010][1010],pr[1010],vis[1010][1010];


int init()
{
    for(int i = 0; i < n; i++)
        G[i].clear();
    edges.clear();
}
int add(int from,int to,int dist)
{
    edges.push_back(Edge(from,to,dist));
    int tm = edges.size();
    G[from].push_back(tm-1);
}
int bfs()
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j <= 100; j++)
            dp[i][j] = INF;
    memset(vis,0,sizeof(vis));
    priority_queue<P> q;
    while(!q.empty())
        q.pop();
    P tmp,tp;
    tmp.u = s;
    tmp.f = 0;
    tmp.cost = 0;
    dp[s][0] = 0;
    q.push(tmp);
    while(!q.empty())
    {
        tmp = q.top();
        q.pop();
        vis[tmp.u][tmp.f] = 1;
        if(tmp.u==t)
        {
            printf("%d\n",tmp.cost);
            return 0;
        }
        if(tmp.f+1<=c&&!vis[tmp.u][tmp.f+1]&&(dp[tmp.u][tmp.f+1]>dp[tmp.u][tmp.f]+pr[tmp.u]))
        {
            dp[tmp.u][tmp.f+1]=dp[tmp.u][tmp.f]+pr[tmp.u];
            tp.f = tmp.f+1;
            tp.u = tmp.u;
            tp.cost = dp[tmp.u][tmp.f+1];
            q.push(tp);
        }
        for(int i = 0; i < G[tmp.u].size(); i++)
        {
            Edge &e = edges[G[tmp.u][i]];
            int u = e.from,v = e.to,w = e.dist;
            if(tmp.f>=w&&!vis[v][tmp.f-w]&&dp[v][tmp.f-w]>tmp.cost)
            {
                dp[v][tmp.f-w] = tmp.cost;
                tp.u = v;
                tp.f = tmp.f-w;
                tp.cost = dp[v][tmp.f-w];
                q.push(tp);
            }
        }
    }
    puts("impossible");
}
int main()
{
    int x,y,z;
    scanf("%d %d",&n,&m);
    init();
    for(int i = 0; i < n; i++)
        scanf("%d",&pr[i]);
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        add(x,y,z);
        add(y,x,z);
    }
    int kc;
    scanf("%d",&kc);
    while(kc--)
    {
        scanf("%d %d %d",&c,&s,&t);
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值