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

Source: Internet
Author: User

Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices have an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game:

Help Greg, print the value of the required sum before each step.
Input

The first line contains an 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,?ai I?=?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.

%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

First off-line, the operation is stored well, and then reverse processing, each time with this point to relax, and then to count the current shortest

/************************************************************************* > File name:cf-179-d.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 27 Monday 14:01 20 seconds ******************************* *****************************************/#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 DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =550; LL Dp[n][n]; LL Ans[n];intOpe[n];BOOLUse[n];intMain () {intN while(~scanf("%d", &n)) {memset(Use,0,sizeof(use)); for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++J) {scanf("%i64d", &dp[i][j]); }        } for(inti =1; I <= N; ++i) {scanf("%d", &ope[i]); } for(intk = n; K >=1; --K) {intp = ope[k]; USE[P] =1; for(inti =1; I <= N; ++i) { for(intj =1; J <= N;                ++J) {Dp[i][j] = min (Dp[i][j], dp[i][p] + dp[p][j]); }} Ans[k] =0; for(inti =1; I <= N; ++i) {if(Use[i]) { for(intj =1; J <= N; ++J) {if(Use[j])                        {Ans[k] + = dp[i][j]; }                    }                }            }        }printf("%i64d", ans[1]); for(inti =2; I <= N; ++i) {printf("%i64d", Ans[i]); }printf("\ n"); }return 0;}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.