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. 小白袍 -- Chapter 1.4.1.1 URL编码的理论解读

    1.4.1.1  URL编码的理论解读 我们在做JavaWeb时避不过GET请求,GET请求和POST请求最大一点不同就在于参数,GET请求的参数会URL中,而POST请求的参数则会在HTTP Hea ...

  2. 跨Vlan通信:单臂路由,三层交换机

    实验涉及命令以及知识补充(涉及Vlan通过的以太网口需要设置为Trunk口) 单臂路由 父接口 no ip address :删除实现单臂路由接口的IP no shutdown 虚拟子接口 R2(co ...

  3. Python基础—16-网络编程

    网络编程 相关概念 OSI七层模型:开放系统互连参考模型.它从低到高分别是:物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. TCP/IP:在OSI七层模型的基础上简化抽象出来的一套网络协 ...

  4. c#数据库连接池Hikari重构升级

    Hikari是我自定义的数据库连接池,前面已经提供了地址.因为c#的连接池按照规范的ADO.NET里面实现定义的.由数据库官方提供,但是实现方式就不知道了,反正没有看出来,估计一般是连接类实现的,但是 ...

  5. Lucene作为一个全文检索引擎

    Lucene作为一个全文检索引擎,其具有如下突出的优点: (1)索引文件格式独立于应用平台.Lucene定义了一套以8位字节为基础的索引文件格式,使得兼容系统或者不同平台的应用能够共享建立的索引文件. ...

  6. Nginx从搭建到配置支持HTTPS

    原文地址:https://www.xingkongbj.com/blog/nginx/nginx.html 安装 基础包 ububtu apt-get install build-essential ...

  7. wordpress整站无损搬迁的几种方法 最后一种最完美

    网站建设之wordpress整站无损搬迁的几种方法 最后一种最完美 网站搬家,当我们更换php虚拟主机,空间升级或更好空间提供商都会发生,站长们请注意,掌握网站迁移方法,是网站日常维护技术中必须掌握的 ...

  8. 监听浏览器返回,pushState,popstate 事件,window.history对象

    在WebApp或浏览器中,会有点击返回.后退.上一页等按钮实现自己的关闭页面.调整到指定页面.确认离开页面或执行一些其它操作的需求.可以使用 popstate 事件进行监听返回.后退.上一页操作. 一 ...

  9. Java实现文件的上传下载

    文件上传,下载的方法: 上传代码 /** * 文件上传.保存 * * @param mapping * @param form * @param request * @param response * ...

  10. ECSHOP和SHOPEX快递单号查询顺丰插件V8.6专版

    发布ECSHOP说明: ECSHOP快递物流单号查询插件特色 本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅 ...