Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)

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 %I64d specifier.

Sample test(s)

Input

1

0

1

Output

0

Input

2

0 5

4 0

1 2

Output

9 0

Input

4

0 3 1 1

6 0 400 1

2 4 0 1

1 1 1 0

4 1 2 3

Output

17 23 404 0

先离线,把操作存好,然后逆序处理,每次都用这个点去松弛一下,然后统计当前的最短路

/*************************************************************************
    > File Name: CF-179-D.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月27日 星期一 14时01分20秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

static const int N = 550;
LL dp[N][N];
LL ans[N];
int ope[N];
bool use[N];

int main() {
    int n;
    while (~scanf("%d", &n)) {
        memset(use, 0, sizeof(use));
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                scanf("%I64d", &dp[i][j]);
            }
        }
        for (int i = 1; i <= n; ++i) {
            scanf("%d", &ope[i]);
        }
        for (int k = n; k >= 1; --k) {
            int p = ope[k];
            use[p] = 1;
            for (int i = 1; i <= n; ++i) {
                for (int j = 1; j <= n; ++j) {
                    dp[i][j] = min(dp[i][j], dp[i][p] + dp[p][j]);
                }
            }
            ans[k] = 0;
            for (int i = 1; i <= n; ++i) {
                if (use[i]) {
                    for (int j = 1; j <= n; ++j) {
                        if (use[j]) {
                            ans[k] += dp[i][j];
                        }
                    }
                }
            }
        }
        printf("%I64d", ans[1]);
        for (int i = 2; i <= n; ++i) {
            printf(" %I64d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-07 04:06:51

Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)的相关文章

Codeforces Round #179 (Div. 2)---C. Greg and Array

Greg has an array a?=?a1,?a2,?-,?an and m operations. Each operation looks as: li, ri, di, (1?≤?li?≤?ri?≤?n). To apply operation i to the array means to increase all array elements with numbers li,?li?+?1,?-,?ri by value di. Greg wrote down k queries

Codeforces Round 261 Div.2 E Pashmak and Graph --DAG上的DP

题意:n个点,m条边,每条边有一个权值,找一条边数最多的边权严格递增的路径,输出路径长度. 解法:先将边权从小到大排序,然后从大到小遍历,dp[u]表示从u出发能够构成的严格递增路径的最大长度. dp[u] = max(dp[u],dp[v]+1),因为有重复的边权值,所以用dis数组先记录,到不重复时一起更新重复的那些边权. 代码: (非原创) #include <iostream> #include <cstdio> #include <cstring> #incl

构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

题目地址 1 /* 2 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 3 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #includ

Codeforces Round #179 (Div. 2) B. Yaroslav and Two Strings (容斥原理)

题目链接 Description Yaroslav thinks that two strings s and w, consisting of digits and having length n are non-comparable if there are two numbers, i andj(1 ≤ i, j ≤ n), such that si > wi and sj < wj. Here sign si represents the i-th digit of string s,

Codeforces Round #179 (Div. 2) B (codeforces 296b) Yaroslav and Two Strings

题目链接:点这里!!!! 题意: 给定两个长度为n(n<=1e5)的串s,w,串中能包含'0'~'9','?','?'可以代替'0'~'9'中任何一个数字. 问你能组成多少对特殊的串('?'代替为数字后的s,w),使得存在(1<=i,j<=n)si<wi,sj>wj. 题解: 1.我们假设'?'的总个数为num,则能够成的总情况为10^num. 2.我们可以通过求相反的情况来求出答案. 3.可以分为4种情况. (1)已知存在(1<=i,j<=n)si<wi,

Codeforces Round #372 (Div. 1) B. Complete The Graph

题目链接:传送门 题目大意:给你一副无向图,边有权值,初始权值>=0,若权值==0,则需要把它变为一个正整数(不超过1e18),现在问你有没有一种方法, 使图中的边权值都变为正整数的时候,从 S 到 T 的最短路恰好等于 L. 若没有输出 "NO",否则输出 "YES",同时输出新图中的所有边权值. 题目思路:二分+最短路(spfa or dijkstra) 闲谈:先%一发杜教,思路来源于看他的代码.然后蒟蒻spfa 982ms,杜教 dijkstra 93m

Codeforces Round #316 (Div. 2) D. Tree Requests 树 离线在线 算法

D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the

Codeforces Round #365 (Div. 2) D 树状数组+离线处理

D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her wit

[Codeforces Round #261 (Div. 2) E]Pashmak and Graph(Dp)

Description Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are given a w