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, NN 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 4 2 1 0 1 3 3 0 1 1 2 2 0 6 6 0 1 0 5 1 3 5 4 3 2 4 2 6 7 0 1 0 5 1 3 1 4 5 4 3 2 4 2  Case #1: 1 Case #2: 1 Case #3: 6 Case #4: 7 

#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) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)