标签:codeforces dp floyd 最短路
给出n个点的完全有权有向图,每次删去一个点,求每次操作前整张图各个点的最短路之和。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#define MAX 507
using namespace std;
typedef long long LL;
LL dp[MAX][MAX],ans[MAX],a[MAX][MAX];
int n,x[MAX];
map<int,int> mp;
int main ( )
{
while (~scanf ( "%d" , &n ))
{
mp.clear();
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= n ; j++ )
scanf ( "%I64d" , &a[i][j] );
for ( int i = 1 ; i <= n ; i++ )
{
scanf ( "%d" , &x[i] );
mp[x[i]] = n+1-i;
}
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= n ; j++ )
dp[mp[i]][mp[j]] = a[i][j];
for ( int k = 1; k <= n ; k++ )
{
for ( int i = 1 ; i <= n ; i++ )
for ( int j = 1 ; j <= n ; j++ )
dp[i][j] = min ( dp[i][j] , dp[i][k] + dp[k][j] );
for ( int i = 1 ; i <= k; i++ )
for ( int j = 1 ; j <= k; j++ )
ans[n-k+1] += dp[i][j];
}
for ( int i = 1 ; i <= n ; i++ )
printf ( "%I64d " , ans[i] );
puts ("");
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 295B B. Greg and Graph(floyd+dp)
标签:codeforces dp floyd 最短路
原文地址:http://blog.csdn.net/qq_24451605/article/details/48544639