Codeforces Round #179 (Div. 1) B
B. Greg and Graph
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:
- The game consists of n steps.
- On the i-th step Greg removes vertex number xi from the graph. As Greg removes a vertex, he also removes all the edges that go in and out of this vertex.
- Before executing each step, Greg wants to know the sum of lengths of the shortest paths between all pairs of the remaining vertices. The shortest path can go through any remaining vertex. In other words, if we assume that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex xi, then Greg wants to know the value of the following sum:
.
Help Greg, print the value of the required sum before each step.
Input
The first line contains integer n (1 ≤ n ≤ 500) — the number of vertices in the graph.
Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line aij (1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertex j.
The next line contains n distinct integers: x1, x2, ..., xn (1 ≤ xi ≤ n) — the vertices that Greg deletes.
Output
Print n integers — the i-th number equals the required sum before the i-th step.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64dspecifier.
题目就是问你,有N个点,构成一张N*N的邻接矩阵表示"i到j的距离",这是一张有向图,我们不断的删除点,删除每个点的同时,该点所连向的所有边,以及该点被连向的所有的边都被删除了,一共删除N个点,问的是每删除这个点之前的整张图的每两个点的最短路的路径大小(i -> j 不等于 j -> i)。
那么,我们正着过去肯定是比较难做的,但是我们如果把删点反过来,不就是一个Floyd求最短路的过程了吗?又因为N只有500,所以O(N^3)的复杂度仍然是可行解。
后删除的点,我们先放进来,然后答案也是反正存的。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 505;
int N, g[maxN][maxN], del[maxN];
ll ans[maxN];
inline void Floyd()
{
for(int i=1; i<=N; i++)
{
int id = del[i];
for(int j=1; j<=N; j++)
{
for(int k=1; k<=N; k++)
{
int x = del[j], y = del[k];
g[x][y] = min(g[x][y], g[x][id] + g[id][y]);
if(j <= i && k <= i) ans[N - i + 1] += g[x][y];
}
}
}
}
int main()
{
scanf("%d", &N);
for(int i=1; i<=N; i++)
{
for(int j=1; j<=N; j++)
{
scanf("%d", &g[i][j]);
}
}
for(int i=N; i>=1; i--) scanf("%d", &del[i]);
Floyd();
for(int i=1; i<=N; i++) printf("%lld%c", ans[i], i == N ? '\n' : ' ');
return 0;
}