UVA 718 - Skyscraper Floors

题目链接

题意:在一个f层高的楼上,有e个电梯,每一个电梯有x,y表示y + k * x层都能够到,如今要问从a层是否能到达b层(中间怎么换乘电梯不限制)

思路:对于两个电梯间能不能换乘,仅仅要满足y[i] + xx x[i] == y[j] + yy y[j].然后移项一下,就能够用拓展欧几里得求解,进而求出x,y的通解,然后利用通解范围x' >= 0, y' >= 0, x[i]
x' + y[i] <= f, x[j] y' + y[j] <= f,求出通解中t的上限和下限,就能够推断有无解了,然后就建图,把两个能够换乘电梯建边,然后在让a, b分别和电梯推断能不能到,能到建边,最后dfs一遍就能推断是否能达到了。

代码:

#include <stdio.h>
#include <string.h>
#include <vector>
#include <math.h>
using namespace std; const int INF = 0x3f3f3f3f;
int t, f, e, a, b, x[105], y[105], vis[105];
vector<int> g[105]; int exgcd(int a, int b, int &x, int &y) {
if (!b) {x = 1; y = 0; return a;}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
} void build(int v, int u, int i) {
if (v < y[i]) return;
if ((v - y[i]) % x[i] == 0) {
g[u].push_back(i);
g[i].push_back(u);
}
} void build2(int i, int j) {
int xx, yy;
int a = x[i], b = -x[j], c = y[j] - y[i];
int d = exgcd(a, b, xx ,yy);
if (c % d) return;
int down = -INF;
int up = INF;
if (b / d > 0) {
down = max(down, (int)ceil(-xx * c * 1.0 / b));
up = min(up, (int)floor(((f - y[i]) * 1.0 * d / x[i] - xx * c * 1.0) / b));
}
else {
down = max(down, (int)ceil(((f - y[i]) * 1.0 * d / x[i] - xx * c * 1.0) / b));
up = min(up, (int)floor(-xx * c * 1.0 / b));
}
if (a / d > 0) {
down = max(down, (int)ceil((yy * c * 1.0 - (f - y[j]) * 1.0 * d / x[j]) / a));
up = min(up, (int)floor(yy * c * 1.0 / a));
}
else {
down = max(down, (int)ceil(yy * c * 1.0 / a));
up = min(up, (int)floor((yy * c * 1.0 - (f - y[j]) * 1.0 * d / x[j]) / a));
}
if (down > up) return;
g[i].push_back(j);
g[j].push_back(i);
} bool dfs(int u) {
if (u == e + 1) return true;
vis[u] = 1;
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (vis[v]) continue;
if (dfs(v)) return true;
}
return false;
} int main() {
scanf("%d", &t);
while (t--) {
memset(g, 0, sizeof(g));
scanf("%d%d%d%d", &f, &e, &a, &b);
for (int i = 1; i <= e; i++) {
scanf("%d%d", &x[i], &y[i]);
build(a, 0, i);
build(b, e + 1, i);
}
for (int i = 1; i <= e; i++) {
for (int j = i + 1; j <= e; j++) {
build2(i, j);
}
}
memset(vis, 0, sizeof(vis));
if (dfs(0)) printf("It is possible to move the furniture.\n");
else printf("The furniture cannot be moved.\n");
}
return 0;
}

UVA 718 - Skyscraper Floors(数论)的更多相关文章

  1. UVA 10627 - Infinite Race(数论)

    UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ...

  2. uva 10555 - Dead Fraction)(数论)

    option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ...

  3. uva 10560 - Minimum Weight(数论)

    题目连接:uva 10560 - Minimum Weight 题目大意:给出n,问说至少须要多少个不同重量的砝码才干称量1~n德重量,给出所选的砝码重量,而且给出k,表示有k个重量须要用上述所选的砝 ...

  4. UVA 11754 - Code Feat(数论)

    UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...

  5. uva 10692 - Huge Mods(数论)

    题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ...

  6. UVA 12009 - Avaricious Maryanna(数论)

    UVA 12009 - Avaricious Maryanna 题目链接 题意:给定一个n.求出n个数位组成的数字x,x^2的前面|x|位为x 思路:自己先暴力打了前几组数据,发现除了1中有0和1以外 ...

  7. uva 11728 - Alternate Task(数论)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36409469 option=com_onli ...

  8. UVa 1393 - Highways(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. uva 11105 - Semi-prime H-numbers(数论)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36644069 option=com_onli ...

随机推荐

  1. web测试 结果存储类型为“Database”,但尚未指定结果储存库连接字符串

    vs2010 Ultimate版带有web测试功能,可以对网站的性能以及负载进行测试. 在进行负载测试时提示“异常 LoadTestConnectStringMissingException 1 Lo ...

  2. oracle 10g函数大全--分析函数

    oracle分析函数--SQL*PLUS环境 一.总体介绍 12.1 分析函数如何工作 语法 FUNCTION_NAME(<参数>,…) OVER (<PARTITION BY 表达 ...

  3. HTML5的本地存储功能,值得研究

    https://developer.chrome.com/apps/offline_storage 搜索“chrome html5 本地缓存”,一大堆文章,比如: http://www.cnblogs ...

  4. eclipse @ 注释为何一写就报错

    以前一直奇怪,为什么eclipse自动生成的的代码中的@注释不会报错,而我直接写@就会报错 原因其实很简单: eclipse会检查@注释的位置 举个例子:写@Override,直接写会报错,但如果你继 ...

  5. 1294 - Positive Negative Sign(规律)

    1294 - Positive Negative Sign   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  6. POJ--1300--Door Man【推断无向图欧拉通路】

    链接:http://poj.org/problem?id=1300 题意:有n个房间.每一个房间有若干个门和别的房间相连.管家从m房间開始走.要回到自己的住处(0),问是否有一条路能够走遍全部的门而且 ...

  7. WinForm界面中快捷键设置

    这是对整个界面的快捷键的设置,比如查询,保存. 1 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if ...

  8. postgresql文档生成注意事项

    如果要生成中文版的postgresql,目前我所知道的方法见我的一篇博客http://www.cnblogs.com/codeblock/p/4812445.html 里面有详细的介绍,但是生成的文档 ...

  9. 循环调用修正sic86

    create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...

  10. keytool 生成 Android SSL 使用的 BKS

    我是在Mac(JDK 1.6) 环境下生成的,Windows  也应该通用; 首先要从CA那里申请来签名的证书,我的是crt格式的; 然后使用如下命令,对应的BcProvider 是 bcprov-e ...