Codeforces 295B B. Greg and Graph (FLOYD+DP)

Source: Internet
Author: User

Topic Links:

Codeforces 295B

Main topic:

Give the total right of n points to the graph, each time the deletion of a point, the entire map before each operation of the shortest point of the sum of the short.

Topic Analysis:
  • First of all, the deletion of the edge is not good for us, so we think of the way to do it by adding a little bit, then how to do it?
  • In fact, we are familiar with the algorithm: Floyd, because we usually use a simplified version of it, so many people may not understand its precise thinking.
  • Before I introduce the concrete solution of this problem, I will explain the Floyd first, and then the problem can be solved.
  • First of all, the Floyd is actually a dynamic programming, and the state is defined when there is no omission. DP[K][I][J] represents the shortest short circuit of the first K points as the medium of the case I to J.
  • The transfer equation is also very simple, as follows: d P[k][I][J]=mIN(d P[k?1][I][J],d P[k?1][I][k]+d P[k?1][k][J])
  • So we'll know. < Span class= "Mi" id= "mathjax-span-4820" style= "font-family:mathjax_math-italic;" >n 3 Recursion, the last dimension is actually labeled as the media point is the first k of the situation, then that is, we want to 1~k these points to make up the shortest path, then because in seeking to get the quickest way, and no use of the latter point as the medium, so is the answer we want, because of the point disorder, So we have to use a hash of the point corresponding to the ordered sequence, and then directly use Floyd do it, do not understand the comments can be asked, I will patiently answer.
AC Code:
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <algorithm>#include <map>#define MAX 507using namespace STD;typedef Long LongLL; LL Dp[max][max],ans[max],a[max][max];intN,x[max]; map<int,int>mpintMain () { while(~scanf("%d", &n)) {mp.clear (); for(inti =1; I <= N; i++) for(intj =1; J <= N; J + +)scanf("%i64d", &a[i][j]); for(inti =1; I <= N; i++) {scanf("%d", &x[i]); Mp[x[i]] = n+1-I.; } for(inti =1; I <= N; i++) for(intj =1; J <= N; J + +) Dp[mp[i]][mp[j]] = a[i][j]; for(intK =1; K <= N; k++) { for(inti =1; I <= N; i++) for(intj =1; J <= N; J + +) Dp[i][j] = min (Dp[i][j], dp[i][k] + dp[k][j]); for(inti =1; I <= K; i++) for(intj =1; J <= K; J + +) ans[n-k+1] + = Dp[i][j]; } for(inti =1; I <= N; i++)printf("%i64d", Ans[i]);puts(""); }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces 295B B. Greg and Graph (FLOYD+DP)

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.