栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 3635 Full Tank?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 3635 Full Tank?

#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#define MAXN 1005#define MAXM 100005#define INF 1000000000using namespace std;struct Edge{    int v, w, next;}edge[MAXM];struct Node{    int v, cost, f;    bool operator <(const Node &a) const{        return a.cost < cost;    }};int head[MAXN], e, n, m, cap;int dp[MAXN][105], used[MAXN][105], p[MAXN];int s, t, ask;priority_queue<Node>q;void init(){    memset(head, -1, sizeof(head));    e = 0;}void ready(){    for(int i = 0; i < n; i++)        for(int j = 0; j <= 100; j++) dp[i][j] = INF;    dp[s][0] = 0;    memset(used, 0, sizeof(used));    while(!q.empty()) q.pop();}void insert(int x, int y, int w){    edge[e].v = y;    edge[e].w = w;    edge[e].next = head[x];    head[x] = e++;}int bfs(){    Node a, b;    a.v = s, a.cost = 0, a.f = 0;    q.push(a);    while(!q.empty())    {        a = q.top();        q.pop();        int u = a.v;        int cost = a.cost;        int f = a.f;        used[u][f] = 1;        if(u == t) return cost;        if(f + 1 <= cap && !used[u][f + 1] && dp[u][f + 1] > dp[u][f] + p[u])        { dp[u][f + 1] = dp[u][f] + p[u]; b.v = u; b.f = f + 1; b.cost = dp[u][f + 1]; q.push(b);        }        for(int i = head[u]; i != -1; i = edge[i].next)        { int v = edge[i].v; int w = edge[i].w; if(f >= w && !used[v][f - w] && dp[v][f - w] > cost) {     dp[v][f - w] = cost;     b.v = v;     b.f = f - w;     b.cost = dp[v][f - w];     q.push(b); }        }    }    return -1;}int main(){    int x, y, w;    scanf("%d%d", &n, &m);    init();    for(int i = 0; i < n; i++)  scanf("%d", &p[i]);    while(m--)    {        scanf("%d%d%d", &x, &y, &w);        insert(x, y, w);        insert(y, x, w);    }    scanf("%d", &ask);    while(ask--)    {        scanf("%d%d%d", ∩, &s, &t);        ready();        int ans = bfs();        if(ans != -1) printf("%dn", ans);        else printf("impossiblen");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/368805.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号