http://codevs.cn/problem/2011/
Title Description Description
The end of the world of DQ planet is coming, but Noah's Ark has not yet been made. In order to make Noah's Ark, all the nations on this planet stand in the United front. There are now a total of n countries, one country to another country has one and only one communication channel, and this channel has a distance, so that the formation of a full map. The Doomsday omens have come, and many of the world's things are being undermined by unexplained causes, including these channels of communication. Now in order to co-create Noah's Ark, it is necessary to count the sum of the shortest distances between all countries (A to B and B to a). The minimum distance between the state of I and J is required. But every once in a while some channels will be destroyed, and now the leaders of DQ planet need you to solve this problem.
Enter a description input Description
For each set of data, the first row is an n, which indicates that there are n countries, followed by n rows, each with n integers. The number in column J of row I indicates the communication channel distance between country I to country j (distance not greater than 10000). Next is a number m, which means that in the foreseeable future there will be m damage to the communication channel, each destruction can only destroy one channel, one channel can be destroyed multiple times, but the first time to destroy this channel will no longer send information. Next there are m lines, two integers a and b per line, indicating that the communication channel from country A to country B is compromised.
outputs description output Description
For each set of data, the output m line, line I indicates the corresponding answer after the break of the first I. If there are two countries that cannot reach each other, output the INF.
sample input to sample
3
0 1 1
1 0 1
1 1 0
4
1 2
1 2
2 3
2 1
sample output Sample outputs
7
7
8
Inf
Data Size & Hint
+%of thenumberaccordingin1<N≤ -,1<m≤ -;
-%of thenumberaccordingin1<N≤ $,1<m≤ $;
Solving
The separation of the subject and the check set is a bit like, are offline reverse processing. Save the answer with a stack, delete all the deleted edges first, then Floyd the shortest distance, press the stack, and use out o ( n 3 ) 。 Then, in reverse order of destruction, the number of times the edge is destroyed is reduced by one per treatment. If the edge is reduced to 0, add this edge to the graph. The distance of all point pairs is updated with this edge as the middle edge for each processing time o ( n 2 ) , the total processing m times. The total time complexity of this algorithm is O((N+m) n 2 ) , 2s is enough.
Code
#include <cstdio>#include <stack>#include <algorithm>using namespace STD;Const intcan =0, oo =1000000000;intNmMap[205][205], f[205][205], des[205][205];structpoint{intx, y; Point (intx =0,inty =0): x (x), Y (y) {}}pnt[205];intTop Stack <int>AnsvoidInit () {intA, B;scanf("%d", &n); for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++J) {scanf("%d", &MapI [j]); F[I][J] =MapI [j]; } }scanf("%d", &m); for(inti =1; I <= m; ++i) {scanf("%d%d", &a, &b); F[a][b] = oo; ++DES[A][B]; Pnt[++top] = Point (A, b); }}intGetans () {intTMP =0; for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++J) {if(I! = J && f[i][j] = = oo) {returnOo }Else{tmp + = F[i][j]; } } }returnTMP;}voidWork () { for(intK =1; K <= N; ++K) { for(inti =1; I <= N; ++i) { for(intj =1; J <= N; ++J) {F[i][j] = min (F[i][j], f[i][k] + f[k][j]); } } } for(inti =1; I <= m; ++i) {Ans.push (Getans ()); --DES[PNT[TOP].X][PNT[TOP].Y];if(DES[PNT[TOP].X][PNT[TOP].Y] = = can) {F[pnt[top].x][pnt[top].y] = min (F[pnt[top].x][pnt[top].y],Map[Pnt[top].x] [Pnt[top].y]); } for(intj =1; J <= N; ++J) { for(intK =1; K <= N; ++K) {F[j][k] = min (F[j][k], f[j][pnt[top].x] + f[pnt[top].x][pnt[top].y] + f[pnt[top].y][k]); }}--top; } while(!ans.empty ()) {if(Ans.top () = = oo) {puts("INF"); }Else{printf("%d\n", Ans.top ()); } ans.pop (); }}intMain () {init (); Work ();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The sum of the minimum distance of LNOI2013