uva 757 - Gone Fishing

本文介绍了一个使用C++实现的求解最短路径问题的算法,包括使用优先队列、堆等数据结构优化算法性能。通过实例演示了如何在给定的输入条件下计算最优路径。
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <queue>
#include <stack>
#include <cmath>
#include <string>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
#define maxn 25 + 10
#define INF 2100000000
#define ll long long
struct node
{
    int no, f, d, t;
    bool operator < (const node &rha) const{
        if(f == rha.f) return no > rha.no;
        else return f < rha.f;
    }
}l[maxn];
priority_queue<node> q;
int main()
{
    int n;
    bool first = true;
    while(scanf("%d", &n) != EOF && n)
    {
        if(first) first = false;
        else puts("");
        int h;
        scanf("%d", &h);
        for(int i = 0; i < n; ++i)
        {
            scanf("%d", &l[i].f);
            l[i].no = i;
        }
        for(int i = 0; i < n; ++i)
            scanf("%d", &l[i].d);
        l[0].t = 0;
        for(int i = 1; i < n; ++i)
        {
            scanf("%d", &l[i].t);
            l[i].t += l[i-1].t;
        }
        int ans = -INF;
        int lt[maxn], anslt[maxn];
        memset(anslt, 0, sizeof(anslt));
        for(int i = 0; i < n; ++i)
        {
            memset(lt, 0, sizeof(lt));
            while(!q.empty()) q.pop();
            for(int j = 0; j <= i; ++j)
                q.push(l[j]);
            int tottime = h*60 - l[i].t*5;
            int sum = 0;
            node t;
            while(tottime > 0)
            {
                if(!q.empty()) t = q.top();
                q.pop();
                if(t.f <= 0) break;
                sum += t.f;
                t.f -= t.d;
                lt[t.no] += 5;
                tottime -= 5;
                q.push(t);
            }
            if(tottime > 0) lt[0] += tottime;
            if(sum > ans)
            {
                ans = sum;
                for(int k = 0; k <= i; ++k)
                    anslt[k] = lt[k];
            }
        }
        printf("%d", anslt[0]);
        for(int i = 1; i < n; ++i)
            printf(", %d", anslt[i]);
        printf("\n");
        printf("Number of fish expected: %d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值