EOJ:Fruit Weights

Fruit Weights

Time Limit: 1000MS Memory Limit: 65536K
Total Submits: 44 Accepted: 10

Description

Have you ever thought about comparing the weight of fruits? That’s what you should do in this problem! Given a series of fruit weight comparisons, you should anticipate the result of another comparison. In this problem, all fruits of the same kind are identical and have the same weights. Each fruit weight comparison is something like “a X ≤ b Y” in which a and b are positive integers, and X and Y are fruit names. Such a comparison means that the weight of a fruits of type X is less than or equal to the weight of b fruits of type Y.

 

Input

The input contains multiple test cases. Each test case starts with a line containing n, which is the number of given comparisons. Each of the next n lines gives a comparison in the form of “a X b Y” meaning that “a X ≤ b Y”. The last line of each test case contains the comparison query in the same form of “a X b Y” inquiring the comparison of “a X” and “b Y”.

A case of n = 0 shows the end of input and should not be processed. All integers in the input (except the last n which is 0) are positive and are not greater than 100. Fruit names are case-sensitive strings of (lowercase and uppercase) letters with length no more than 50.

 

Output

For each test case, write one line with your result for that test case. Your result can be one of the followings (assume the comparison query was “a X b Y”):
* “<=”: meaning you are sure that “a X ≤ b Y”.
* “>=”: meaning you are sure that “a X ≥ b Y”.
* “==”: meaning you are sure that “a X = b Y” (i.e. you have reached both of the above results).
* “UNAVAILABLE”: meaning that you can say nothing for sure in comparing “a X” and “b Y” (i.e. you have reached none of the above results).
* “INCONSISTENT”: meaning that there is an inconsistency in the given comparisons (i.e. you are sure that all the given comparisons for that test case cannot hold at the same time).

 

Sample Input

2
2 Orange 3 Apple
1 Apple 1 Peach
2 Orange 3 Peach
1
2 Orange 3 Apple
2 Orange 2 Apple
2
3 a 2 A
2 A 3 a
5 A 5 a
2
3 B 2 A
2 A 3 B
2 A 3 B
3
2 b 2 A
2 A 2 C
3 C 2 b
1 A 1 b
0

 

Sample Output

<=
UNAVAILABLE
>=
==
INCONSISTENT
原题地址:http://www.cn210.com/onlinejudge/problemshow.php?pro_id=223
__________________________________________________________________________________________________________
题解:

精妙的转化

 

N种水果,现知道许多以下的关系: aX<=bY 

表示:aX水果的重量小于b个水果Y的重量。给出许多这些小于关系后,最后问aX水果和bY水果的重量关系。水果的数目不超过一百。 

 

这个问题可以转化成图论问题考虑。视每个水果为一个节点,对于关系aX<=bY,我们可以建立一条从YX的边,权值为a/b,意思是Y水果的单位重量至少是X水果的a/b倍。 

然后使用floyd算法求一次最短路,将加法改成乘法即可。算出每种水果之间的重量比例关系。 

检查算出来的矩阵,如果有g[i][i]>1,那么就是出现矛盾,判为INCONSISTENT。 

如果要判定aX是否<=bY,也就是判定Y>=(a/b)X。对于算出的矩阵,g[Y][X]表示Y>=g[Y][X]X。若判定Y>=(a/b)X成立,必有(a/b)<=G[Y][X]。 

对于相等的情况特判一下即可。 

 

 

 

代码
1 #include<stdio.h>
2 #include<string.h>
3 #include<memory.h>
4 #include<algorithm>
5  using namespace std;
6 #define eps 1e-8
7 char name[400][60];
8 char as[60],bs[60],str[60];
9 int a,b,i,j,k,t,n,xx,yy,flag;
10 char temp[50];
11 double map[400][400];
12 struct node
13 {
14 int w;
15 char s[60];
16 }x[400],y[400];
17 double max(double a,double b)
18 {
19 if (a>b) return a;
20 return b;
21 }
22 void swap(int i,int j)
23 {
24 strcpy(temp,name[i]);
25 strcpy(name[i],name[j]);
26 strcpy(name[j],temp);
27 }
28 void qsort(int l,int r)
29 {
30 int i,j;
31 i=l-1;
32 for (j=l;j<r;j++)
33 if (strcmp(name[j],name[r])<=0)
34 {
35 i++;
36 swap(i,j);
37 }
38 swap(i+1,r);
39 if (i+2<r) qsort(i+2,r);
40 if (i>l) qsort(l,i);
41 }
42 int find(char x[50])
43 {
44 int l,r,m;
45 l=1;
46 r=t;
47 while (l<=r)
48 {
49 m=(l+r)/2;
50 if (strcmp(name[m],x)==0) return m;
51 if (strcmp(name[m],x)<0) l=m+1;
52 else r=m-1;
53 }
54 }
55 int main()
56 {
57 freopen("a.txt","w",stdout);
58 while(scanf("%d",&n)!=EOF&&n)
59 {
60 memset(map,0,sizeof(map));
61 t=0;
62 for (i=1;i<=n;i++)
63 {
64 scanf("%d%s%d%s",&x[i].w,x[i].s,&y[i].w,y[i].s);
65 strcpy(name[++t],x[i].s);
66 strcpy(name[++t],y[i].s);
67 }
68 qsort(1,t);
69 j=1;
70 strcpy(str,name[1]);
71 for (i=2;i<=t;i++)
72 if (strcmp(str,name[i])!=0)
73 {
74 j++;
75 strcpy(name[j],name[i]);
76 strcpy(str,name[i]);
77 }
78 t=j;
79 for (i=1;i<=n;i++)
80 {
81 a=find(x[i].s);
82 b=find(y[i].s);
83 map[b][a]=max(map[b][a],(double)x[i].w/y[i].w);
84 }
85 for (i=1;i<=t;i++)
86 map[i][i]=1;
87 for (k=1;k<=t;k++)
88 for (i=1;i<=t;i++)
89 for (j=1;j<=t;j++)
90 if (map[i][k]>0&&map[k][j]>0)
91 map[i][j]=max(map[i][j],map[i][k]*map[k][j]);
92 flag=0;
93 for (i=1;i<=t;i++)
94 if (map[i][i]>1)
95 {
96 flag=1;
97 break;
98 }
99 scanf("%d%s%d%s",&a,as,&b,bs);
100 xx=find(as);
101 yy=find(bs);
102 if (flag==1)
103 printf("INCONSISTENT\n");
104 else
105 {
106 if (map[yy][xx]>=(double)a/b-eps&&map[xx][yy]>=(double)b/a-eps) printf("==\n");
107 else if (map[yy][xx]>=(double)a/b-eps) printf("<=\n");
108 else if (map[xx][yy]>=(double)b/a-eps) printf(">=\n");
109 else printf("UNAVAILABLE\n");
110 }
111 }
112 return 0;
113 }
114
115
116
117
118

 

posted on 2010-07-28 14:45  风也轻云也淡  阅读(183)  评论(0编辑  收藏  举报