USACO Section 3.2: Feed Ratios

直接暴力搜

 1 /*

 2 ID: yingzho1

 3 LANG: C++

 4 TASK: ratios

 5 */

 6 #include <iostream>

 7 #include <fstream>

 8 #include <string>

 9 #include <map>

10 #include <vector>

11 #include <set>

12 #include <algorithm>

13 #include <stdio.h>

14 #include <queue>

15 #include <cstring>

16 #include <cmath>

17 #include <list>

18 #include <cstdio>

19 #include <cstdlib>

20 

21 using namespace std;

22 

23 ifstream fin("ratios.in");

24 ofstream fout("ratios.out");

25 

26 struct node {

27     int x, y, z;

28     node(int a, int b, int c) : x(a), y(b), z(c) { }

29     node() : x(0), y(0), z(0) { }

30 };

31 

32 const int inf = 10000;

33 

34 //int f[101][101][101];

35 

36 int main()

37 {

38     node target, s[3];

39     fin >> target.x >> target.y >> target.z;

40     for (int i = 0; i < 3; i++)

41         fin >> s[i].x >> s[i].y >> s[i].z;

42     /*for (int i = 0; i < 101; i++)

43         for (int j = 0; j < 101; j++)

44             for (int k = 0; k < 101; k++)

45                 f[i][j][k] = inf;*/

46     int minnum = inf;

47     int recx, recy, recz;

48     for (int i = 0; i < 101; i++) {

49         for (int j = 0; j < 101; j++) {

50             for (int k = 0; k < 101; k++) {

51                 int xx = i*s[0].x+j*s[1].x+k*s[2].x;

52                 int yy = i*s[0].y+j*s[1].y+k*s[2].y;

53                 int zz = i*s[0].z+j*s[1].z+k*s[2].z;

54                 if ((target.x == 0 || xx%target.x == 0) && (target.y == 0 || yy%target.y == 0) && (target.z == 0 || zz%target.z == 0) && xx*target.y == yy*target.x && yy*target.z == zz*target.y) {

55                     int tmp = inf;

56                     if (target.x && xx) tmp = xx/target.x;

57                     else if (target.y && yy) tmp = yy/target.y;

58                     else if (target.z && zz) tmp = zz/target.z;

59                     if (tmp < minnum) {

60                         minnum = tmp;

61                         recx = i, recy = j, recz = k;

62                     }

63                 }

64             }

65         }

66     }

67     if (minnum < inf) fout << recx << " " << recy << " " << recz << " " << minnum << endl;

68     else fout << "NONE" << endl;

69 

70     return 0;

71 }

 

你可能感兴趣的:(USACO)