Description |
秒速 5 厘米,那是樱花飘落的速度,那么怎样的速度才能走完我与你之间的距离? 如果有其他阻碍的话,撕毁就可以了吧? 给定一张 N 个点的完全图,每条边都是有向带权的。 N 轮操作,操作会对之后的图产生影响。定义 Dis(x,y)表示 x 到 y 的最短距离 第 i 轮删掉点 Si,询问当前图中的 其中 i,j 为现存的节点。 |
Input |
多组数据 对于每一组数据: 第一行输入数据组数 T 对于每一组数据,第一行输入点数 n 之后 n 行每行 n 个整数,A[i][j]表示从 i 到 j 的路径距离,保证 A[i][i]为 0 之后一行 n 个数表示每次删掉的点 |
Output |
对于每组数据:一行 n 个数,表示删完第 i 个点之前的 Ans |
Sample Input |
1 4 0 3 1 1 6 0 400 1 2 4 0 1 1 1 1 0 4 1 2 3 |
Sample Output |
17 23 404 0 |
【官方题解】:
删点不太好做,但我们可以考虑到这做使得这个过程变成加点。
用类似Floyd的方式,O(N^2)更新新加入的点带来的影响即可。
希望能快点把题放出来吧,方便我补题。。。。
#include<bits/stdc++.h>
using namespace std;
const int N = 305;
int G[N][N],a[N];
int main()
{
int n,T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&G[i][j]);
}
}
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
reverse( a+1, a+1+n);
int ans[N]={0};
int cnt=0;
for(int k=1;k<=n;k++){
int tmp = 0;
for(int i=1;i<=k;i++){
for(int j=1;j<=k;j++){
G[a[i]][a[j]] = min( G[a[i]][a[j]] , G[a[i]][a[k]]+G[a[k]][a[j]] );
tmp += G[a[i]][a[j]];
}
}
ans[cnt++]=tmp;
}
reverse( ans, ans+cnt);
for(int i=0;i<cnt;i++){
printf("%d\n",ans[i]);
}
}
return 0;
}