/* 题目描述: (题目也太长了 废话太多了 抄的累人呀) 王伯退休后开始养鱼。他一早起来就赶去动物公园, 发现这个世界的鱼真不少,五光十色、色彩斑斓,大的、小的 什么都有。这些鱼实在是太美了,买的人越来越多,湖里的鱼 越来越少。没有美丽的鱼,哪里有美丽的湖(这个逻辑。。?)? 于是动物园不得不规定,对于每种鱼,每个人最多只能买一条。 并且有些鱼是不能一起买的,因为他们之间会互相争斗吞食。 王伯想买尽可能多的鱼,但很可惜,他的资金有限。他冥思苦想、 不知如何是好。请编写一个程序帮助他。如果有多个方案都能买尽可能多的鱼, 选择所花资金最多的一个。(不知道为什么要花钱做多的一个而不是最少的那个)。。 输入格式: 从输入文件读入数据。输入文件的第一行为两个正整数M(M<=1000),N(N<=30), 分别表示王伯的资金和鱼的种类。一下N行,每行有两个正整数S(1<=S<=N),T,分别 表示某种鱼的编号以及该鱼的价格。 接着,每行有两个整数P,Q。当P,Q均大于0时,表示P,Q不能共处;当P,Q均等于0时, 表示输入文件的结束。 输出格式: 输出文件的第一行为两个正整数X,Y,分别表示所卖鱼的条数和总花费。以下X行, 每行均有一个正整数,表示所美誉的编号。编号安生序排列输出。 如果题目有多个解,只需输出一个解。 由于输入输出样例太多 这里只给出三个 SAMPLE 1: ====================================================================== Sample Input Output for the input 170 7 4 160 1 70 2 2 50 4 3 30 5 4 40 6 5 40 6 30 7 20 1 4 1 7 3 4 3 5 5 7 6 7 0 0 ====================================================================== SAMPLE 2: ====================================================================== Sample Input Output for the input 14 3 0 0 1 34 2 15 3 97 1 2 0 0 ====================================================================== SAMPLE 3: ====================================================================== Sample Input Output for the input 1000 10 5 702 1 47 1 2 276 5 3 91 7 4 127 8 5 224 10 6 76 7 344 NOTICE:我觉得这个答案有误 8 9 我得道的结果是 9 179 =========================== 10 78 5 746 10 6 3 9 7 5 9 1 7 8 2 8 7 6 10 7 4 =========================== 7 2 题目中是花费要最大,估计答案当作最小花费算了。。。 6 3 5 3 5 2 3 1 0 0 ====================================================================== */ /* @author aa_niaofang 2011-4-15 12:04:28 */ #include <stdio.h> #include <stdlib.h> /*Global variable*/ int count=0; int totalPrice=0; int *result=NULL; void fishSearch(int,int,int*,int**,int *); int num(int*,int); int pri(int*,int,int *); void main() { int mony,kinds,i=0,j=0; printf("Input the mony of Uncle Wang and the kinds of the fish/n"); scanf("%d%d",&mony,&kinds); int *price=(int *)malloc(sizeof(int)*kinds); int *search=(int *)malloc(sizeof(int)*kinds); result=(int *)malloc(sizeof(int)*kinds); int **just=(int **)malloc(sizeof(int*)*kinds); for(i=0;i<kinds;i++) { just[i]=(int *)malloc(sizeof(int)*kinds); search[i]=0; result[i]=0; } for(i=0;i<kinds;i++) for(j=0;j<kinds;j++) { just[i][j]=0; } printf("Input the price of each fish:/n"); for(i=0;i<kinds;i++) { int k; scanf("%d%d",&k,&price[i]); } printf("input the imcompatible fishes ends with 0 0:/n"); while(1) { scanf("%d%d",&i,&j); if(i==0 && j==0) break; just[i-1][j-1]=1; just[j-1][i-1]=1; } fishSearch(mony,kinds,price,just,search); printf("%d %d/n",count,totalPrice); for(i=0;i<kinds;i++) { if(result[i]==1) printf("%d/n",i+1); } } void fishSearch(int mony,int kinds,int *price,int **just,int*search) { for(int i=0;i<kinds;i++) { if(search[i]==0) { int flag=0; for(int j=0;j<kinds;j++) { if(search[j] && just[j][i]==1) { flag=1; break; } } if(flag==0) { if(pri(search,kinds,price)+price[i]<=mony) { search[i]=1; fishSearch(mony,kinds,price,just,search); search[i]=0; } } } } if(num(search,kinds)>count) { count=num(search,kinds); totalPrice=pri(search,kinds,price); for(int k=0;k<kinds;k++) { result[k]=search[k]; } } if(num(search,kinds)==count) { if(pri(search,kinds,price)>totalPrice) { totalPrice=pri(search,kinds,price); for(int k=0;k<kinds;k++) { result[k]=search[k]; } } } } int num(int *search,int kinds) { int count=0; for(int i=0;i<kinds;i++) { if(search[i]==1) count++; } return count; } int pri(int *search,int kinds,int *price) { int n=0; for(int i=0;i<kinds;i++) { if(search[i]==1) n+=price[i]; } return n; }