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).

题目大意:给n条aX ≤ bY的不等式,其中X、Y是未知数的符号,a、b是常数。最后给出aX ? bY,要求判断 ? 是哪个符号。如果相等输出==,大于等于输出>=,小于等于输出<=,不能判断输出UNAVAILABLE,n条不等式不可能同时成立输出INCONSISTENT。

思路:首先X、Y是各种各样的字符串,所以先离散化,C++里面的map<string, int>是不错的选择。然后我们就要考虑,不同的不等式,该怎样才能联立起来。比如2a ≤ 3b,5b ≤ 7c,这样就会确定了一个关于a和c的不等式,然而我们须要表现在代码里面,不是很容易做到。

所以移向,2/3 a ≤ b, 5/7 b ≤ c,如果 c 到 b 连一条边,权值为 5/7; b 到 a 连一条边,权值为 2/3。那么,我们从 c 走到 a,就可以得到 (5/7 * 2/3) * a ≤ c。如果有假设提问是x * a ? y * c,那么对所有x/y ≤ (5/7 * 2/3),因为若p ≤ q,有pa ≤ qa ≤ qc。

然后对每一条边,取最大的权值。因为若有两条边pa ≤ pc,qa ≤ qc,且p ≤ q,那么我们只要保留qa ≤ qc,那么我们就可以推断出pa ≤ pc。

根据上述推论,我们用floyd来直接求出最长路径,mat[j][i]代表mat[j][i] * i ≤ j。n个不等式冲突,就说明存在mat[i][i] > 1,令mat[i][i] * i ≤ i无法成立,输出INCONSISTENT。对询问x * i ? y * j,若有y/x = mat[i][j]且x/y = mat[j][i],那么有x * i = y * j。若有y/x ≤ mat[i][j],则有x * i ≥ y * j。若 x/y ≤ mat[j][i],则有x * i ≤ y * j。若前面都不成立,则说明无法判断,输出UNAVAILABLE。

对于询问x * i ? y * j, i 或 j 前面都没有出现过的,好像是没有这种情况,别人没管这个也能AC。

PS:之前闲得无聊写了一个分数类结果RE了,大概是乘起来太大然后求约数的时候被零除跪了,好吧这不是重点。

PS2:最短路径的那个代码就是把原来的mat[j][i] * i ≤ j换成了i ≤ j * mat[j][i],思路差不多。

代码(125MS)(最长路径):

 #include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXN = ;
const double EPS = 1e-; map<string, int> mp;
string a, b;
int m, n; double x, y;
int aid, bid;
double mat[MAXN][MAXN]; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} bool floyd() {
for(int k = ; k <= n; ++k)
for(int i = ; i <= n; ++i) if(mat[i][k] > )
for(int j = ; j <= n; ++j) if(mat[k][j] > )
if(mat[i][k] * mat[k][j] > mat[i][j]) mat[i][j] = mat[i][k] * mat[k][j];
/*
for(int k = 1; k <= n; ++k)
for(int i = 1; i <= n; ++i) if(mat[i][k] > 0)
for(int j = 1; j <= n; ++j) if(mat[k][j] > 0)
if(mat[i][j] > 0 && sgn(mat[i][k] * mat[k][j] - mat[i][j]) == 1) return false;*/
for(int i = ; i <= n; ++i)
for(int j = i + ; j <= n; ++j) {
if(mat[i][j] < || mat[j][i] < ) continue;
if(mat[i][j] * mat[j][i] > ) return false;
}
return true;
} int main() {
ios::sync_with_stdio(false);
while(cin>>m) {
if(m == ) break;
n = ;
mp.clear();
for(int i = ; i < MAXN; ++i) {
for(int j = ; j < MAXN; ++j) mat[i][j] = -;
mat[i][i] = ;
}
for(int i = ; i < m; ++i) {
cin>>x>>a>>y>>b;
if(mp.find(a) != mp.end()) aid = mp[a];
else mp[a] = aid = ++n;
if(mp.find(b) != mp.end()) bid = mp[b];
else mp[b] = bid = ++n;
//if(mat[aid][bid] < double(x, y)) mat[aid][bid] = double(x, y);
if(mat[bid][aid] < x / y) mat[bid][aid] = x / y;
}
cin>>x>>a>>y>>b;
if(!floyd()) {
puts("INCONSISTENT");
continue;
}
if(mp.find(a) == mp.end() || mp.find(b) == mp.end()) {
puts("UNAVAILABLE");
continue;
}
aid = mp[a];
bid = mp[b];
if(sgn(mat[aid][bid] - y / x) == && sgn(mat[bid][aid] - x / y) == ) {
puts("==");
continue;
}
if(mat[bid][aid] > && sgn(mat[bid][aid] - x / y) >= ) {
puts("<=");
continue;
}
if(mat[aid][bid] > && sgn(mat[aid][bid] - y / x) >= ) {
puts(">=");
continue;
}
puts("UNAVAILABLE");
}
}

代码(125MS)(最短路径):

 #include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXN = ;
const double EPS = 1e-; map<string, int> mp;
string a, b;
int m, n; double x, y;
int aid, bid;
double mat[MAXN][MAXN]; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} bool floyd() {
for(int k = ; k <= n; ++k)
for(int i = ; i <= n; ++i) if(mat[i][k] > )
for(int j = ; j <= n; ++j) if(mat[k][j] > )
if(mat[i][j] < || mat[i][k] * mat[k][j] < mat[i][j]) mat[i][j] = mat[i][k] * mat[k][j];
for(int i = ; i <= n; ++i)
for(int j = i + ; j <= n; ++j) {
if(mat[i][j] < || mat[j][i] < ) continue;
if(sgn(mat[i][j] * mat[j][i] - ) < ) return false;
}
return true;
} int main() {
ios::sync_with_stdio(false);
while(cin>>m) {
if(m == ) break;
n = ;
mp.clear();
for(int i = ; i < MAXN; ++i) {
for(int j = ; j < MAXN; ++j) mat[i][j] = -;
mat[i][i] = ;
}
for(int i = ; i < m; ++i) {
cin>>x>>a>>y>>b;
if(mp.find(a) != mp.end()) aid = mp[a];
else mp[a] = aid = ++n;
if(mp.find(b) != mp.end()) bid = mp[b];
else mp[b] = bid = ++n;
if(aid == bid) continue;
//if(mat[aid][bid] < double(x, y)) mat[aid][bid] = double(x, y);
if(mat[bid][aid] < || mat[bid][aid] > y / x) mat[bid][aid] = y / x;
}
cin>>x>>a>>y>>b;
if(!floyd()) {
puts("INCONSISTENT");
continue;
}
if(mp.find(a) == mp.end() || mp.find(b) == mp.end()) {
puts("UNAVAILABLE");
continue;
}
aid = mp[a];
bid = mp[b];
if(sgn(mat[aid][bid] - x / y) == && sgn(mat[bid][aid] - y / x) == ) {
puts("==");
continue;
}
if(mat[bid][aid] > && sgn(mat[bid][aid] - y / x) <= ) {
puts("<=");
continue;
}
if(mat[aid][bid] > && sgn(mat[aid][bid] - x / y) <= ) {
puts(">=");
continue;
}
puts("UNAVAILABLE");
}
}

POJ 3860 Fruit Weights(数学+最长路径 or 最短路径)的更多相关文章

  1. POJ 1797 Heavy Transportation(Dijkstra变形——最长路径最小权值)

    题目链接: http://poj.org/problem?id=1797 Background Hugo Heavy is happy. After the breakdown of the Carg ...

  2. 【POJ 3162】 Walking Race (树形DP-求树上最长路径问题,+单调队列)

    Walking Race   Description flymouse's sister wc is very capable at sports and her favorite event is ...

  3. Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)

    In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, an ...

  4. ubuntu 终端设置(颜色与长路径)

    Linux给人最大的享受就是可以根据个人喜好去定制令自己舒服的系统配置,像终端颜色的设置就是一个典型的例子. 图1 系统默认状态下的终端显示     在没有经过自定义配置的终端下工作久了,难免容易疲劳 ...

  5. Codefroces Gym 100781A(树上最长路径)

    http://codeforces.com/gym/100781/attachments 题意:有N个点,M条边,问对两两之间的树添加一条边之后,让整棵大树最远的点对之间的距离最近,问这个最近距离是多 ...

  6. poj 2533 Longest Ordered Subsequence 最长递增子序列

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098562.html 题目链接:poj 2533 Longest Ordered Subse ...

  7. hdoj 2196 Computer【树的直径求所有的以任意节点为起点的一个最长路径】

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. AOE网上的关键路径(最长路径 + 打印路径)

    题目描述 一个无环的有向图称为无环图(Directed Acyclic Graph),简称DAG图.     AOE(Activity On Edge)网:顾名思义,用边表示活动的网,当然它也是DAG ...

  9. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

随机推荐

  1. MySQL里面的锁

    MySQL里面的锁可以分为:全局锁,表级锁,行级锁. 一.全局锁:对整个数据库实例加锁.MySQL提供加全局读锁的方法:Flush tables with read lock(FTWRL)这个命令可以 ...

  2. .net 导出Excel插件Npoi的使用

    1.NuGet搜索Npoi并安装 2.添加引用将包引用进来 3.Controller里引用 4.使用 public ActionResult ExportExcel() { plist = 数据源 H ...

  3. 2018 Wannafly summer camp Day8--区间权值

    区间权值 小Bo有\(n\)个正整数\(a_1\)--\(a_n\),以及一个权值序列\(w_1\)--\(w_n\),现在她定义\(f(l,r)=(\sum_{i=l}^r a_i^2) *w_{r ...

  4. [codevs1036] 商务旅行

    题目描述 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任 ...

  5. Java分享笔记:创建多线程 & 线程同步机制

    [1] 创建多线程的两种方式 1.1 通过继承Thread类创建多线程 1.定义Thread类的子类,重写run()方法,在run()方法体中编写子线程要执行的功能. 2.创建子线程的实例对象,相当于 ...

  6. Maven里面多环境下的属性过滤(配置)

    情景:通常一个项目都为分为开发环境(dev)和测试环境(test)还有正式环境(prod),如果每次一打包都要手动地去更改配置文件,例如数据库连接配置.将会很容易出差错. 解决方案:maven pro ...

  7. jquery点击li 获取当前父节点所在类的索引

    jquery点击li 获取当前父节点所在类的索引 $('.jbcz').find('.content li').click(function(){ //alert($('.jbcz').find('. ...

  8. Array-快餐管饱

    一.如何获得一个数组? rsp: 1. []  2.new Array() 3.str.split() ps:new Array()可以不加括号,其传一个参数代表数组长度,两个及以上就是初始化数组. ...

  9. Discuz论坛搜索下拉框插件openSug

    Discuz!只需安装openSug插件即可获得带有“搜索框提示”功能的搜索框,让您的Discuz搜索更便捷! 下载:https://www.opensug.org/faq/.../opensug.d ...

  10. Python面向对象的类的操作

    import randomimport time class ElectronicCoupon(): def __init__(self): self.__ecid=time.strftime('%Y ...