ZOJ1161-Gone Fishing

 

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章