CF350E.Wrong Floyd

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Valera conducts experiments with algorithms that search for shortest paths. He has recently studied the Floyd's algorithm, so it's time to work with it.

Valera's already written the code that counts the shortest distance between any pair of vertexes in a non-directed connected graph from n vertexes and m edges, containing no loops and multiple edges. Besides, Valera's decided to mark part of the vertexes. He's marked exactly k vertexes _a_1, _a_2, ..., a__k.

Valera's code is given below.

ans[i][j] // the shortest distance for a pair of vertexes i, j
a[i] // vertexes, marked by Valera

for(i = 1; i <= n; i++) {
for(j = 1; j <= n; j++) {
if (i == j)
ans[i][j] = 0;
else
ans[i][j] = INF; //INF is a very large number
}
}

for(i = 1; i <= m; i++) {
read a pair of vertexes u, v that have a non-directed edge between them;
ans[u][v] = 1;
ans[v][u] = 1;
}

for (i = 1; i <= k; i++) {
v = a[i];
for(j = 1; j <= n; j++)
for(r = 1; r <= n; r++)
ans[j][r] = min(ans[j][r], ans[j][v] + ans[v][r]);
}

Valera has seen that his code is wrong. Help the boy. Given the set of marked vertexes _a_1, _a_2, ..., a__k, find such non-directed connected graph, consisting of n vertexes and m edges, for which Valera's code counts the wrong shortest distance for at least one pair of vertexes (i, j). Valera is really keen to get a graph without any loops and multiple edges. If no such graph exists, print -1.

瓦列拉正在对最短路径搜索算法进行实验。他最近学习了 Floyd 算法,现在是时候动手实践了。

瓦列拉已经编写了一段代码,用于计算一个包含 nn 个顶点、mm 条边的无向连通图中任意两个顶点之间的最短距离;该图不含自环(loop)和重边(multiple edges)。此外,瓦列拉还决定标记其中一部分顶点,恰好标记了 kk 个顶点:a1, a2, …, aka_1,\,a_2,\,\dots,\,a_k。

瓦列拉的代码如下所示:

ans[i][j] // 顶点 i 与 j 之间的最短距离  
a[i]      // 瓦列拉所标记的顶点  
  
for(i = 1; i <= n; i++) {  
 for(j = 1; j <= n; j++) {  
 if (i == j)  
 ans[i][j] = 0;  
 else  
 ans[i][j] = INF; // INF 是一个极大的数   
 }  
}   
  
for(i = 1; i <= m; i++) {  
 读入一条无向边连接的两个顶点 u, v;  
 ans[u][v] = 1;  
 ans[v][u] = 1;  
}  
  
for (i = 1; i <= k; i++) {  
 v = a[i];  
 for(j = 1; j <= n; j++)  
 for(r = 1; r <= n; r++)  
 ans[j][r] = min(ans[j][r], ans[j][v] + ans[v][r]);  
}  

瓦列拉发现自己的代码存在错误。请帮助这位少年:给定被标记的顶点集合 a1, a2, …, aka_1,\,a_2,\,\dots,\,a_k,构造一个由 nn 个顶点和 mm 条边组成的无向连通图,使得瓦列拉的代码对该图中至少一对顶点 (i, j)(i,\,j) 计算出的最短距离是错误的。瓦列拉非常希望得到一个不含自环和重边的图。若不存在这样的图,请输出 -1。

输入格式

The first line of the input contains three integers n, m, k (3 ≤ n ≤ 300, 2 ≤ k ≤ n , ) — the number of vertexes, the number of edges and the number of marked vertexes.

The second line of the input contains k space-separated integers _a_1, _a_2, ... a__k (1 ≤ a__i ≤ n) — the numbers of the marked vertexes. It is guaranteed that all numbers a__i are distinct.

输入的第一行包含三个整数 nn、mm、kk(3 ≤ n ≤ 3003 \le n \le 300,2 ≤ k ≤ n2 \le k \le n,)——分别表示顶点数、边数和标记顶点数。

输入的第二行包含 kk 个以空格分隔的整数 a1, a2, …, aka_1,\,a_2,\,\dots,\,a_k(1 ≤ ai ≤ n1 \le a_i \le n)——表示被标记的顶点编号。保证所有 aia_i 互不相同。

输出格式

If the graph doesn't exist, print -1 on a single line. Otherwise, print m lines, each containing two integers u, v — the description of the edges of the graph Valera's been looking for.

如果这样的图不存在,则在一行中输出 -1。否则,输出 m 行,每行包含两个整数 u, v —— 描述 Valera 所寻找的图的各条边。

输入输出样例

  • 输入#1

    3 2 2
    1 2

    输出#1

    1 3
    2 3
  • 输入#2

    3 3 2
    1 2

    输出#2

    -1

输入解题思路,AI测评打分。不知道怎么写?

首页