CodeForces 773D. Perishable Roads

来源:互联网 发布:网络消费者投诉来分期 编辑:程序博客网 时间:2024/06/05 01:18

In the country of Never, there are n cities and a well-developed road system. There is exactly one bidirectional road between every pair of cities, thus, there are as many as  roads! No two roads intersect, and no road passes through intermediate cities. The art of building tunnels and bridges has been mastered by Neverians.

An independent committee has evaluated each road of Never with a positive integer called the perishability of the road. The lower the road's perishability is, the more pleasant it is to drive through this road.

It's the year of transport in Never. It has been decided to build a museum of transport in one of the cities, and to set a single signpost directing to some city (not necessarily the one with the museum) in each of the other cities. The signposts must satisfy the following important condition: if any Neverian living in a city without the museum starts travelling from that city following the directions of the signposts, then this person will eventually arrive in the city with the museum.

Neverians are incredibly positive-minded. If a Neverian travels by a route consisting of several roads, he considers the perishability of the route to be equal to the smallest perishability of all the roads in this route.

The government of Never has not yet decided where to build the museum, so they consider all n possible options. The most important is the sum of perishabilities of the routes to the museum city from all the other cities of Never, if the travelers strictly follow the directions of the signposts. The government of Never cares about their citizens, so they want to set the signposts in a way which minimizes this sum. Help them determine the minimum possible sum for all n possible options of the city where the museum can be built.

Input

The first line contains a single integer n (2 ≤ n ≤ 2000) — the number of cities in Never.

The following n - 1 lines contain the description of the road network. The i-th of these lines contains n - i integers. The j-th integer in the i-th line denotes the perishability of the road between cities i and i + j.

All road perishabilities are between 1 and 109, inclusive.

Output

For each city in order from 1 to n, output the minimum possible sum of perishabilities of the routes to this city from all the other cities of Never if the signposts are set in a way which minimizes this sum.

Examples
input
31 23
output
223
input
62 9 9 6 67 1 9 109 2 54 108
output
6575711
Note

The first example is explained by the picture below. From left to right, there is the initial road network and the optimal directions of the signposts in case the museum is built in city 1, 2 and 3, respectively. The museum city is represented by a blue circle, the directions of the signposts are represented by green arrows.

For instance, if the museum is built in city 3, then the signpost in city 1 must be directed to city 3, while the signpost in city 2 must be directed to city 1. Then the route from city 1 to city 3 will have perishability 2, while the route from city 2 to city 3 will have perishability 1. The sum of perishabilities of these routes is 3.





题意:给一个完全图,对于每个点求出一棵以它为根的内向树,使得所有点到它距离之和最小

定义距离为i到j在树上经过的边的边权的最小值

题解:

智商题喵啊

性质1:存在一个最优解是一条链

否则如果A->C,B->C,可以改成B->A->C或者A->B->C不会更劣

性质2:把所有边都减去min{边权},最后答案加上它的n-1倍,这样一定有一条0的边

可以证明,把最后的链提取出来,边权记为w[1]...w[n - 1]则有w[i]>w[i+1]对于i <= k - 3恒成立(w[k] = 0且k最小)

即一定是一条递减的链,最后可能是递减/连一条边到0链的端点,否则把那个w[i]<=w[i+1]的位置的下个位置直接接到k上答案不会更劣

第一种是直接统计边权和,第二种是用两次2*某条边的边权

然后可以dijkstra辣~

#include <bits/stdc++.h>#define xx first#define yy second#define mp make_pair#define pb push_back#define fill( x, y ) memset( x, y, sizeof x )#define copy( x, y ) memcpy( x, y, sizeof x )using namespace std;typedef long long LL;typedef pair < int, int > pa;inline int read(){int sc = 0, f = 1; char ch = getchar();while( ch < '0' || ch > '9' ) { if( ch == '-' ) f = -1; ch = getchar(); }while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();return sc * f;}const int MAXN = 2005;const int INF = INT_MAX;int n, a[MAXN][MAXN], m = INF, d[MAXN];bool v[MAXN];int main(){#ifdef wxh010910freopen( "data.in", "r", stdin );#endifn = read();for( int i = 1 ; i <= n ; i++ )for( int j = i + 1 ; j <= n ; j++ )a[ i ][ j ] = a[ j ][ i ] = read(), m = min( a[ i ][ j ], m );for( int i = 1 ; i <= n ; i++ )for( int j = i + 1 ; j <= n ; j++ )a[ i ][ j ] = a[ j ][ i ] -= m;for( int i = 1 ; i <= n ; i++ ){d[ i ] = INF;for( int j = 1 ; j <= n ; j++ ) if( i ^ j ) d[ i ] = min( d[ i ], a[ i ][ j ] << 1 );}d[ 0 ] = INF;for( int i = 1 ; i <= n ; i++ ){int p = 0;for( int j = 1 ; j <= n ; j++ ) if( !v[ j ] && d[ j ] < d[ p ] ) p = j;for( int j = 1 ; j <= n ; j++ ) d[ j ] = min( d[ j ], d[ p ] + a[ p ][ j ] );v[ p ] = 1;}for( int i = 1 ; i <= n ; i++ ) printf( "%lld\n", 1LL * ( n - 1 ) * m + d[ i ] );}


0 0
原创粉丝点击