用户输出
13 1
系统信息
Exited with return code 0
编号 | 题目 | 状态 | 分数 | 总时间 | 内存 | 代码 / 答案文件 | 提交者 | 提交时间 |
---|---|---|---|---|---|---|---|---|
#19242 | #1115. 2-11G. JM的玄学旅社 | Accepted | 100 | 3201 ms | 122580 K | C++ 11 / 2.6 K | JM233333 | 2019-07-18 23:42:53 |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
class Node {
public:
int id;
int dist;
Node(int v, int d) : id(v), dist(d) {}
Node() {}
friend bool operator>(const Node& A, const Node& B) { return (A.dist > B.dist); }
};
void add_edge(int u, int v);
void bfs(int st);
const int INF = 0x3f3f3f3f;
const int MAX = 3005;
vector<int> graph[MAX];
bool visited[MAX];
int dist[MAX][MAX];
vector<Node> dist_from[MAX], dist_to[MAX];
int main() {
// freopen("test.txt", "r", stdin);
int n, m;
while (scanf("%d %d", &n, &m) != EOF) {
// 初始化
for (int i = 0; i < MAX; i++) {
graph[i].clear();
dist_from[i].clear();
dist_to[i].clear();
}
memset(dist, INF, sizeof(dist));
// 输入
int u, v;
while (m--) {
scanf("%d %d", &u, &v);
add_edge(u, v);
}
// BFS
for (int u = 1; u <= n; u++) {
bfs(u);
}
// 预处理
for (int u = 1; u <= n; u++)
for (int v = 1; v <= n; v++) {
if (u == v) {
continue;
}
if (dist[u][v] != INF)
dist_from[u].push_back(Node(v, dist[u][v]));
if (dist[v][u] != INF)
dist_to[u].push_back(Node(v, dist[v][u]));
}
for (int u = 1; u <= n; u++) {
sort(dist_from[u].begin(), dist_from[u].end(), greater<Node>());
sort(dist_to[u].begin(), dist_to[u].end(), greater<Node>());
}
// 求解
int res = 0;
int A, B, C, D;
for (int b = 1; b <= n; b++)
for (int c = 1; c <= n; c++) {
if (b == c || dist[b][c] == INF) {
continue;
}
for (int i = 0; i < min(3, (int)dist_to[b].size()); i++) {
int a = dist_to[b][i].id;
if (a == b || a == c) {
continue;
}
for (int j = 0; j < min(3, (int)dist_from[c].size()); j++) {
int d = dist_from[c][j].id;
if (d == a || d == b || d == c) {
continue;
}
int sum = dist[a][b] + dist[b][c] + dist[c][d];
if (res < sum) {
res = sum;
A = a;
B = b;
C = c;
D = d;
}
}
}
}
// 输出
// printf("%d %d %d %d\n", A, B, C, D);
printf("%d %d\n", res, B);
}
return 0;
}
// 加入新边
inline void add_edge(int u, int v) { graph[u].push_back(v); }
// 广度优先搜索
void bfs(int st) {
memset(visited, false, sizeof(visited));
queue<int> q;
q.push(st);
visited[st] = true;
dist[st][st] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if (!visited[v]) {
q.push(v);
visited[v] = true;
dist[st][v] = dist[st][u] + 1;
}
}
}
}
用户输出
13 1
系统信息
Exited with return code 0
用户输出
4 1
系统信息
Exited with return code 0
9 20
2 5
3 2
3 4
4 8
4 4
4 3
4 3
4 3
5 9
5 2
5 1
6 2
6 3
7 1
7 9
7 7
8 9
8 9
9 4
<6 bytes omitted>
用户输出
13 2
系统信息
Exited with return code 0
16 23
1 5
2 16
2 6
2 5
3 16
5 6
5 10
5 5
6 4
6 4
6 9
8 10
9 3
11 2
11 12
11 9
12 14
<38 bytes omitted>
用户输出
14 2
系统信息
Exited with return code 0
4 35
1 1
1 1
1 1
1 4
1 2
1 1
1 1
1 1
1 1
2 4
2 2
2 1
2 4
2 2
2 1
2 4
2 2
2 2
2 3
<81 bytes omitted>
用户输出
5 1
系统信息
Exited with return code 0
8 29
1 7
1 2
1 6
1 6
1 7
2 3
2 2
2 1
3 7
3 6
3 7
4 3
4 8
5 4
5 4
5 7
5 7
6 1
6 3
<51 bytes omitted>
用户输出
12 2
系统信息
Exited with return code 0
用户输出
5 3
系统信息
Exited with return code 0
155 2891
34 112
28 62
145 48
30 66
28 4
99 31
4 70
104 53
131 135
5 59
37 127
66 124
43 115
41 30
6
<18932 bytes omitted>
用户输出
9 1
系统信息
Exited with return code 0
648 4723
6 120
344 200
1 442
58 89
324 164
255 306
393 556
174 596
361 139
568 580
267 9
231 242
335
<36170 bytes omitted>
用户输出
18 37
系统信息
Exited with return code 0
1975 2882
318 170
1821 301
728 505
721 913
1365 1157
1459 672
1950 242
1371 55
1072 809
141 1068
140
<25524 bytes omitted>
用户输出
108 1168
系统信息
Exited with return code 0
2032 3056
315 1657
1844 659
46 236
1835 675
342 746
961 1216
915 523
203 592
1170 938
127 108
217 18
<27211 bytes omitted>
用户输出
98 647
系统信息
Exited with return code 0
3000 5000
2649 200
2603 967
889 1881
1332 2220
1266 1396
400 716
593 478
2106 133
2768 1251
2796 118
<46247 bytes omitted>
用户输出
94 1152
系统信息
Exited with return code 0
3000 5000
181 130
509 499
1771 1501
131 2975
790 62
2583 1938
1035 773
1538 1806
2633 295
406 2217
1
<46264 bytes omitted>
用户输出
91 602
系统信息
Exited with return code 0
2990 3500
1211 1075
539 2609
1676 2371
223 1964
1203 2386
1811 1609
1890 188
2224 1397
732 2718
596
<32270 bytes omitted>
用户输出
93 241
系统信息
Exited with return code 0
3000 4000
1511 2959
567 2183
1026 1949
2034 1021
2852 1665
2441 2170
2503 2261
188 2565
1823 2033
14
<36964 bytes omitted>
用户输出
17 30
系统信息
Exited with return code 0
3000 2999
2712 2915
2294 1910
2794 1980
1940 697
1981 1447
792 2721
150 740
252 166
547 64
1571 2866
<27686 bytes omitted>
用户输出
12 184
系统信息
Exited with return code 0
500 5000
62 170
473 455
341 121
117 343
447 160
436 125
323 48
331 468
343 108
279 250
333 475
121 1
<37757 bytes omitted>
用户输出
15 17
系统信息
Exited with return code 0
1000 1000
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
1
<8697 bytes omitted>
用户输出
2997 1
系统信息
Exited with return code 0