[CF295B]Greg and Graph 题解

[CF295B]Greg and Graph 题解

题目地址:Codeforces:Problem – 295B – Codeforces、洛谷:【CF295B】Greg and Graph – 洛谷

题目描述

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: \sum_{v, u, v \neq u} d(i, v, u).

Help Greg, print the value of the required sum before each step.
给一个完全图的邻接矩阵,按顺序删点,求每一次删点前剩下的点两两最短路长度的和。

输入输出格式

输入格式:
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.

输出格式:
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 %I64d specifier.

输入输出样例

输入样例#1:

1
0
1

输出样例#1:

0

输入样例#2:

2
0 5
4 0
1 2

输出样例#2:

9 0

输入样例#3:

4
0 3 1 1
6 0 400 1
2 4 0 1
1 1 1 0
4 1 2 3

输出样例#3:

17 23 404 0 

题解

n的范围很小,用O(n^2)的枚举求和是可行的,但是删点这个就不怎么好办了。
正难则反,如果把删点改成加点是否可以呢?
多源最短路让我们想到了Floyd,其实Floyd的实质是一个DP,由于压过维,所以有点看不出,实际上这个式子可以写成这样
dp[k][i][j]= \min (dp[k-1][i][j], dp[k-1][i][k] + dp[k-1][k][j])
如果在这个意义下,我们发现第一维维k的时候指的是只有1~k的图中的最短路。我们考虑把给出的排列反着来跑,计算出x[k]~x[n]的图的最短路,再把这些点的最短路每个算一遍加起来,加进答案序列。

代码

// Code by KSkun, 2018/3
#include <cstdio>

#include <algorithm>

typedef long long LL;

inline char fgc() {
    static char buf[100000], *p1 = buf, *p2 = buf;
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++;
}

inline LL readint() {
    register LL res = 0, neg = 1;
    char c = fgc();
    while(c < '0' || c > '9') {
        if(c == '-') neg = -1;
        c = fgc();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = fgc();
    }
    return res * neg;
}

const int MAXN = 505;

int n, dis[MAXN][MAXN], x[MAXN];
LL ans[MAXN];

int main() {
    n = readint();
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++) {
            dis[i][j] = readint();
        }
    }
    for(int i = 1; i <= n; i++) {
        x[i] = readint();
    }
    for(int k = n; k >= 1; k--) {
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= n; j++) {
                dis[i][j] = std::min(dis[i][j], dis[i][x[k]] + dis[x[k]][j]);
            }
        }
        for(int i = n; i >= k; i--) {
            for(int j = n; j >= k; j--) {
                ans[k] += dis[x[i]][x[j]];
            }
        }
    }
    for(int i = 1; i <= n; i++) {
        printf("%I64d ", ans[i]);
    }
    return 0;
}


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据