1
2 //n 1 //h 10 1 //fi 2 5 //di 2 //ti |
4 4 10 15 20 17 0 3 4 3 1 2 3 |
4 4 10 15 50 30 0 3 4 3 1 2 3 0 |
完整程序:
#include<bits/stdc++.h>
using namespace std;
//ZOJ1161-Gone Fishing贪心算法
//每个湖预计钓到鱼的数量,定义为数组:
#define NUM 30
int f[NUM];
//每个湖预计钓到鱼的数量的递减值,定义为数组:
int d[NUM];
//相邻湖之间的旅行时间,定义为数组:
int t[NUM];
//钓鱼计划,定义为数组:
int plan[NUM];
//湖的个数n,用于钓鱼的时间h,尽可能多的钓鱼数量best。
int n,h,best;
//选择鱼最多的湖钓鱼的贪心算法实现
//从湖1起到湖pos止,花费时间time(不含路程)的钓鱼计划
void greedy(int pos, int time)
{
if (time <= 0) return;//时间已经用完
int i, j;
int fish[NUM];
int p[NUM];
int t = 0;
for (i = 0; i < pos; ++i)
fish[i] = f[i];
memset(p, 0, sizeof(p));
//在时间time内,选择鱼最多的湖钓鱼;如果鱼都没有了,就把时间放在湖1上
for (i = 0; i < time; ++i)
{
int max = 0; //鱼最多的湖中,鱼的数量
int id = -1;//鱼最多的湖的编号
//查找鱼最多的湖中,鱼的数量和湖的编号
for (j = 0; j < pos; ++j)
if (fish[j] > max){
max = fish[j];
id = j;
}
if (id != -1)//找到了,进行钓鱼处理
{
++p[id];
fish[id] -= d[id];
t += max;
}
//没有找到(从湖1起到湖pos全部钓完了),就把时间放在湖1上
else ++p[0];
}
//处理最优方案
if (t > best)
{
best = t;//最优值
memset(plan, 0, sizeof(plan));
for (i = 0; i < pos; ++i)//最优解
plan[i] = p[i];
}
}
int main()
{
cout<<"请输入湖的个数:"<<endl;
while(scanf("%d",&n),n){
cout<<"请输入钓鱼的时间:"<<endl;
cin>>h;
cout<<"请输入每个湖预计钓到的鱼的数量:"<<endl;
for(int j=0;j<n;j++){
cin>>f[j];
}
cout<<"请输入每个湖预计钓到鱼的数量的递减值:"<<endl;
for(int j=0;j<n;j++){
cin>>d[j];
}
cout<<"请输入在两个湖之间的路程时间:"<<endl;
for(int j=1;j<n;j++){
cin>>t[j];
}
int time=0;//花费在路程上的时间
h=h*60/5;
best = -1;
for(int k=1;k<=n&&h-time;++k){
greedy(k,h-time);
time+=t[k];
}
for (int i = 0; i < n - 1; ++i)
printf("%d, ", plan[i] * 5);
printf("%d\n", plan[n - 1] * 5);
printf("Number of fish expected: %d\n", best);
cout<<"请输入湖的个数:"<<endl;
}
return 0;
}