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)