列表

详情


NC236065. Shortest Path Fast Algorithm

描述

Recently, BaoBao has learned the Shortest Path Fast Algorithm (SPFA, or more formally, Bellman-Ford-Moore Algorithm) to solve the shortest path problem efficiently. He realizes that the algorithm looks so similar to the Dijkstra's algorithm after replacing the FIFO queue with priority queue, and shows you the below pseudo code.

By picking the best vertex from Q we mean picking the vertex with the smallest priority value (in case that multiple vertices have the smallest priority value, pick the vertex with the largest index among them).
You, the future computer scientist, find the BaoBao-modified SPFA algorithm works so slow in some carefully construted graph. However, BaoBao is sure that his algorithm works well, unless you show him a simple undirected graph that makes the variable cnt in the SPFA function no less than a certain k at some time. For convenience the source vertex of the SPFA function is specified to be vertex 1.
Just teach him a lesson!

输入描述

There is only one test case in each test file.
The first and only line of the input contains a single integer k where for the sample test case and for the only secret test case.

输出描述

Output several lines in the following format to describe the input data of a simple undirected graph that makes the variable cnt in the SPFA function no less than k at some time.
The first line contains two integers n () and m (), indicating the number of vertices and edges in the graph.
Then m lines follow, the i-th of which contains three integers u_i, v_i () and w_i (), indicating that the i-th edge in the graph has a weight of w_i and connects the u_i-th and the v_i-th vertices.
Note that a simple graph contains no self-loops and no multiple edges.

示例1

输入:

1

输出:

4 6
1 2 1
2 3 2
3 4 3
4 1 4
1 3 5
2 4 6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 384K, 提交时间: 2022-10-04 10:59:05

#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
	puts("100 165");
	int inf=900000;
	for (int i=1;i<=99;i+=3){
		printf("%d %d %d\n",i,i+1,inf);
		printf("%d %d %d\n",i,i+2,1);
		printf("%d %d %d\n",i+1,i+3,1);
		inf=max(1,inf/2-1);
		printf("%d %d %d\n",i+2,i+3,inf);
		printf("%d %d %d\n",i+1,i+2,1);
	}
}

上一题