您的位置:首页 > 其它

EOJ - 我决不会TLE (一个智障的题目)

2018-01-22 17:27 253 查看
Time limit per test: 2.5 seconds

Memory limit: 256 megabytes

题目描述

xxx 写了一份用 DFS 求有向无环图中顶点 1 到 n 最短路的代码,出乎意料的是这份代码竟然通过了所有测试点。于是你暗地里把出题人骂了一通,然后决定造个数据把这个假算法卡掉。

核心代码如下:

global variable: answer_now

function dfs(u, distance_now)
if distance_now >= answer_now then
return
if u == n then
answer_now = distance_now
return
for each u->v in edges
dfs(v, dist + 1)

function find_shortest_path()
answer_now = INFINITY
dfs(1, 0)
return answer_now


Output

第一行两个数 n,m,分别表示图中点的个数和边的条数。(1≤n≤50,1≤m≤100)

之后 m 行,每行两个数 u,v,表示顶点 u→v 有一条有向边。 (1≤u,v≤n)

要求:

存在 1 到 n 的路径。

不能有重边。

不能有环。

xxx 的算法会给出错误解或者运行超时(时限是 2 秒)。

Examples

input

Sample

output

7 8

6 7

1 2

1 3

2 4

2 5

3 5

4 6

5 6

Note

样例给出了一种可能的输出(不是正确的输出)。

题解

看完这个题目真的是摸不着头脑,怪事年年有,这是让我干嘛???我怎么输出???

因为第一次做这么智障的题目,我想把这个题目记录下来。

如标题嘛,主要就是卡算法的时间,一秒约摸着执行10^6~10^7次代码吧。因为对于这个样例算法,只要让他最短路有多解,他的执行效率是最低的,所以,我们在中间层加很多的等路径的点。例如 1 和 n 之间加入 24 个中间层,每层两个顶点,前一层的顶点与后一层的顶点连边。在这种情况下,1 到 n 的最短路条数有 2^24 种。

代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
srand( (unsigned)time( NULL ) );
int n=rand()%7+44;//n的取值范围[44,50]
cout<<n<<" "<<2*n-4<<endl;
cout<<1<<" "<<2<<endl;
cout<<1<<" "<<3<<endl;
for(int i=2;i<n-2;i++){
if(i%2){//odd
cout<<i<<" "<<i+1<<endl;
cout<<i<<" "<<i+2<<endl;
}else{
cout<<i<<" "<<i+2<<endl;
cout<<i<<" "<<i+3<<endl;
}
}
cout<<n-2<<" "<<n<<endl;
cout<<n-1<<" "<<n<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  构造
相关文章推荐