Problem A. Floyd-Warshall

Author:StdAlg   Time limit:1 sec
Input file:input.txt   Memory limit:8 Mb
Output file:output.txt  

Statement

You are to write a program that finds shortest distances between all pairs of vertices in a directed weighted graph. Graph consists of N vertices, numbered from 1 to N, and M edges.

Input file format

Input file contains two integers N and M, followed my M triplets of integers ui vi wi — starting vertex, ending vertex and weight or the edge. There is at most one edge connecting two vertices in every direction. There are no cycles of negative weight.

Output file format

Output file must contain a matrix of size NxN. Element in the j-th column of i-th row mush be the shortest distance between vertices i and j. The distance from the vertex to itself is considered to be 0. If some vertex is not reachable from some other, there must be empty space in corresponding cell of matrix.

Constraints

0 ≤ N ≤ 100. All weights are less than 1000 by absolute value.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
3 3
1 2 5
1 3 10
2 3 2
0 5 7
  0 2
    0

Problem B. Multiplication puzzle

Author:Far-Eastern Subregional   Time limit:1 sec
Input file:input.txt   Memory limit:8 Mb
Output file:output.txt  

Statement

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring 10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000

If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be 1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

Input file format

The first line of the input file contains the number of cards N. The second line contains N integers ai, separated by spaces.

Output file format

Output file must contain a single integer - the minimal score.

Constraints

3 ≤ N ≤ 100, 1 ≤ ai ≤ 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
6
10 1 50 50 20 5
3650

Задача C. Триангуляция пирога

Автор:Известная   Ограничение времени:4 сек
Входной файл:input.txt   Ограничение памяти:64 Мб
Выходной файл:output.txt  

Условие

Праздничный пирог имеет форму неправильного выпуклого многоугольника с N вершинами. Хозяйка хочет порезать его на N − 2 кусков. Причем ее высокие эстетические идеалы требуют, чтобы куски имели форму треугольников. Разрезы же должны проходить через вершины изначального многоугольника и не пересекаться.

Как показывает практика, чем длиннее разрез, тем больше драгоценной начинки остается на ноже. Поэтому, чтобы минимизировать накладные расходы, желательно разделить пирог так, чтобы суммарная длина разрезов была минимальна.

Напишите программу, которая решает эту задачу.

Формат входного файла

Первая строка входного файла содержит число N — количество вершин многоугольника.

Вторая строка содержит N пар разделенных пробелами целых чисел xi yi — координаты вершин многоугольника в порядке обхода по часовой стрелке.

Формат выходного файла

В выходной файл выведите минимальную суммарную длину разрезов с точностью до четырех знаков после запятой.

Ограничения

3 ≤ N ≤ 100

Примеры тестов

Входной файл (input.txt) Выходной файл (output.txt)
1
6
0 0 1 1 2 1 3 0 2 -1 1 -1
6.2361

0.046s 0.003s 11