Maximize the distance in graph

4.7

3 votes
Algorithms, Graphs, Hard, Roy Floyd, Shortest Path Algorithm
Problem

This is an approximate probem.

Given an undirected complete graph G with n vertices. The weight of the edge between (i,j) is equal to C[i][j]. Let D(i,j) be the shortest distance between vertices i,j and W(G) be the sum of all shortest distances between any two vertices. You can remove at most k edges from graph G in order to obtain a graph G'. You have to maximize the ratio of the sum of all shortest distances between any two vertices in new graph G' (after removing the edges) and sum of all shortest distances between any two vertices in old graph G (before reomoving the edges): W(G)W(G). G must remain connected.

Input

The first line contains two space separated integers n (n=128) and k (0k(n1)2) denoting the number of vertices and at most how many edges you can remove.

Next n lines contains n space separated integers. ith line denotes the elements of C[i] i.e.

C[i][1],C[i][2],,C[i][n] (C[i][i]=0,C[i][j]=C[j][i],1C[i][j]220).

Output

The first line contains a number q (0qk) - the number of edges deleted.

Next q lines contains two space separated integers each,  xi,yi (1xi<yin) denoting that edge (xi,yi) will be deleted.

Scoring

The score will be equal to W(G)W(G).

Tests

The value k will be taken uniformly from interval [0,(n1)2].

The value C[i][j] (1i<jn) will be taken uniformly from interval [1,220].

Sample Input
3 1
0 1 1
1 0 1
1 1 0
Sample Output
1
1 2
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

We delete vertex (1,2) and obtain that D(1,2)=2, D(2,3)=1 and D(1,3)=1 so the total sum is equal to D(1,2)+D(2,1)+D(2,3)+D(3,2)+D(1,3)+D(3,1)=2(D(1,2)+D(2,3)+D(3,1))=2(2+1+1)=8.

Editor Image

?