The triple loop we wrote in those years -- CodeForces 295B Greg and Graph review the Floyd algorithm

Source: Internet
Author: User
Greg and Graphtime limit per test

3 seconds

Memory limit per test

256 megabytes

Input

Standard input

Output

Standard output

Greg has a weighed directed graph, consistingNVertices. 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 published Ted a new game:

  • The game consistsNSteps.
  • OnI-Th step Greg removes vertex numberXIFrom
    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 thatD(I, Bytes,V, Bytes,U) Is
    The shortest path between verticesVAndUIn
    The graph that formed before deleting vertexXI,
    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 integerN(1 digit ≤ DigitNLimit ≤ limit 500 )-
The number of vertices in the graph.

NextNLines containNIntegers
Each-the graph adjacency matrix:J-Th number inI-Th
LineAIj(1 digit ≤ DigitAIjLimit ≤ limit 105, limit,AIiBytes = bytes 0) represents
The weight of the edge that goes from vertexITo vertexJ.

The next line containsNDistinct integers:X1, bytes,X2, middle..., middle ,...,XN(1 digit ≤ DigitXILimit ≤ limitN)-
The vertices that Greg deletes.

Output

PrintNIntegers-I-Th
Number equals the required sum beforeI-Th step.

Please, do not use the % lld specifier to read or write 64-bit integers in C ++.
It is preferred to use thecin, cout streams
Of the % I64d specifier.

Sample test (s) input
101
Output
0 
Input
20 54 01 2
Output
9 0 
Input
40 3 1 16 0 400 12 4 0 11 1 1 04 1 2 3
Output
17 23 404 0 

Link: here

This is a classic question worth doing. It reminds us of the light behind the triple for loop of the Floyd algorithm.


#include<cstdio>#define N 505#define ll long long intll dp[N][N],arr[N],ans[N],n;ll _min(ll a,ll b) {    return a<b?a:b;}int main(){    while(scanf("%d",&n)!=EOF) {        for(int i=0;i<n;i++) {            for(int j=0;j<n;j++) scanf("%lld",&dp[i][j]);        }        for(int i=0;i<n;i++) scanf("%lld",&arr[n-i-1]);        for(int i=0;i<n;i++) arr[i]--;        for(int mid=0;mid<n;mid++) {            int u=arr[mid];            ll temp=0;            for(int e=0;e<n;e++) {                for(int s=0;s<n;s++) {                    int a=arr[s];                    int b=arr[e];                    dp[a][b]=_min(dp[a][b],dp[a][u]+dp[u][b]);                    if(s<=mid && e<=mid) temp+=dp[a][b];                }            }            ans[n-mid-1]=temp;        }        for(int i=0;i<n;i++) printf("%I64d ",ans[i]);        printf("\n");    }    return 0;}

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.