UVA 10985 - Rings'n'Ropes(floyd)
Problem D
Rings'n'Ropes
Time Limit: 3 seconds
"Well, that seems to be the situation. But, I don't want that, and you don't want that, and Ringo here definitely doesn't want that." |
Jules Winnfield
I have n tiny rings made of steel. I also have m pieces of rope, all of exactly the same length. The two ends of each piece of rope are tied to two different rings.
I am going to take one of the rings, L, into my left hand, and another ring, R into my right hand. Then I will pull the whole structure apart as hard as I can. Some of the ropes will be streched horizontally because of this. Others will hang down or bend out of shape. If I want the number of horizontally stretched ropes to be as large as possible, which L and R should I pick?
Assume that the stretching of ropes in negligible, they all have negligible thickness and are free to slide around the rings that they are tied to. The thickness and radius of each ring is negligible, too.
Input
The first line of input gives the number of cases, N. N test cases follow. Each one starts with two lines containing n (
2<=
n<=120
) and m(
0<=
m<=
n(
n-1)/2
). The next m lines will each contain a pair of different rings (integers in the range [0, n-1]). Each pair of rings will be connected by at most one rope.
Output
For each test case, output the line containing "Case #x:", followed by the largest number of ropes that I can stretch horizontally by picking a pair of rings, L and R.
Sample Input | Sample Output |
|
|
题意:有n个戒指,中间连着m条绳子,现在要求出选定两个戒指,拉直之后,中间有多少绳子被绷直,求出绷直绳子最多的绳子数
思路:最短路,先用floyd打出整个最短路表,然后枚举两点,把两点间满足最短路的所有点都找出来,然后还要进行一个判断,如果起点到两个点的距离是相等的话,那么这条边是无法被拉直的。
代码:
- #include <stdio.h>
- #include <string.h>
- #define INF 0x3f3f3f3f
- const int N = 155;
- int T, n, m, cas = 0;
- int a, b;
- int g[N][N], f[N][N];
- void init() {
- memset(g, 0, sizeof(g));
- memset(f, INF, sizeof(f));
- scanf("%d%d", &n, &m);
- for (int i = 0; i < m; i ++) {
- scanf("%d%d", &a, &b);
- f[a][b] = f[b][a] = 1;
- g[a][b] = g[b][a] = 1;
- }
- for (int i = 0; i < n; i ++)
- f[i][i] = 0;
- for (int k = 0; k < n; k ++)
- for (int i = 0; i < n; i ++)
- for (int j = 0; j < n; j ++) {
- if (f[i][j] > f[i][k] + f[k][j])
- f[i][j] = f[i][k] + f[k][j];
- }
- }
- void solve() {
- init();
- int ans = 0;
- for (int u = 0; u < n; u ++)
- for (int v = u + 1; v < n; v ++) {
- int save[N], num = 0, count = 0;
- for (int i = 0; i < n; i ++) {
- if (f[u][v] == f[u][i] + f[i][v])
- save[num ++] = i;
- }
- for (int i = 0; i < num; i ++)
- for (int j = i + 1; j < num; j ++) {
- if (g[save[i]][save[j]] && f[u][save[i]] != f[u][save[j]])
- count ++;
- }
- if (count > ans)
- ans = count;
- }
- printf("Case #%d: %d\n", ++cas, ans);
- }
- int main() {
- scanf("%d", &T);
- while (T --) {
- solve();
- }
- return 0;
- }
UVA 10985 - Rings'n'Ropes(floyd)的更多相关文章
- UVA 10985 Rings'n'Ropes
最短路 参考了Staingger的博客 感觉DP的状态记录还是有毛病.可以DFS寻找结果也. #include <map> #include <set> #include &l ...
- UVa 247 - Calling Circles(Floyd求有向图的传递闭包)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 436 - Arbitrage (II)(floyd)
UVA 436 - Arbitrage (II) 题目链接 题意:给定一些国家货币的汇率.问是否能通过不断换货币使钱得到增长 思路:floyd,完事后推断一下有没有连到自己能大于1的情况 代码: #i ...
- UVa 1001 Say Cheese【floyd】
题意:在一个三维的奶酪里面有n(n<=100)个洞,老鼠A想到达老鼠B的位置, 在洞里面可以瞬间移动,在洞外面的移动速度为10秒一个单位,求最短时间 看到n<=100,又是求最短时间,想到 ...
- uva 104 Arbitrage (DP + floyd)
uva 104 Arbitrage Description Download as PDF Background The use of computers in the finance industr ...
- UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)
题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...
- Uva(10048),最短路Floyd
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVa 821 Page Hopping【Floyd】
题意:给出一个n个点的有向图,任意两个点之间都相互到达,求任意两点间最短距离的平均值 因为n很小,所以可以用floyd 建立出图,然后用floyd,统计d[][]不为0且不为INF的边的和及条数,就可 ...
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ...
随机推荐
- 24种设计模式--装饰模式【Decorator Pattern】
装饰模式在中国使用的那实在是多,中国的文化是中庸文化,说话或做事情都不能太直接,需要有技巧的,比如说话吧,你要批评一个人,你不能一上来就说你这个做的不对,那个做的不对,你要先肯定他的成绩,表扬一下优点 ...
- php解决下单、抽奖并发导致的库存负数的问题
我们知道数据库处理sql是一条条处理的,假设购买商品的流程是这样的: sql1:查询商品库存 if(库存数量 > 0) { //生成订单... sql2:库存-1 } 当没有并发 ...
- jquery自动切换tabs选项卡
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncodi ...
- ExecuteReader
最近在做winform的编程,想到一真没有使用过ExecuteReader.可能以前以后它的用户不大,或者 不大好用,故没有用过.今天在这里将学习记录写下来,供读者参考: 1.MSDN上说:Sends ...
- python使用pyapns进行ios推送消息
Pyapns 提供了通用的Apple Push Notification Service (APNS).该解决方案使用了开源的Twisted server,支持原生的Python和Ruby API. ...
- Java实现Http服务器(三)
下面重点介绍上篇文章介绍的HttpServerImpl类当中的ServerImpl类 sun.net.httpserver.ServerImpl 600行左右的类,是整个HttpServer的核心 ...
- Asp.net MVC的Controller激活理解【学习笔记】
DefaultControllerFactory 是MVC默认的Controller查找和激活工厂类我们可以通过自定义ControllerFactory替换DefaultControllerFacto ...
- matlab的二维卷积操作
MATLAB的conv2函数实现步骤(conv2(A,B)): 其中,矩阵A和B的尺寸分别为ma*na即mb*nb ① 对矩阵A补零,第一行之前和最后一行之后都补mb-1行,第一列之前和最后一列之后都 ...
- LightOj_1284 Lights inside 3D Grid
题目链接 题意: 给一个X * Y * Z 的立方体, 每个单位立方体内都有一盏灯, 初始状态是灭的, 你每次操作如下: 1)选择一个点(x1, y1, z1) 再选择一个点(x2, y2, ...
- http server v0.1_http_parse.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "mime.h&q ...