Greg has a weighed directed graph, consisting of n vertices. 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 invented a new game: The game consists of n steps. On the i-th step Greg removes vertex number xi from 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 that d(i, v, u) is the shortest path between vertices v and u in the graph that formed before deleting vertex xi, 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 integer n (1 ≤ n ≤ 500) — the number of vertices in the graph. Next n lines contain n integers each — the graph adjacency matrix: the j-th number in the i-th line aij (1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertex j. The next line contains n distinct integers: x1, x2, …, xn (1 ≤ xi ≤ n) — the vertices that Greg deletes. Output Print n integers — the i-th number equals the required sum before the i-th step. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. Examples Input 1 0 1 Output 0 Input 2 0 5 4 0 1 2 Output 9 0 Input 4 0 3 1 1 6 0 400 1 2 4 0 1 1 1 1 0 4 1 2 3 Output 17 23 404 0 题意;有n点有边权有向图,第i次操作删除图中的d[i]号节点,问每次删除的点前剩余的点对应的最短路径的权值的和。 问每次剩余点的最短路径的和,可以用逆序的思维,从最后一个开始计算,慢慢加到第一个。 可以自己先画一个图 应该是一个完全图 带有四个环 若将其余三点全删掉,则最短路径是0(自己到自己) 经典的floyd 求最短路径 套用模板即可
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; long long vis[100100],dis[501][501],ans[100100],d[100100]; int main() { long long n; long long i,j; while(~scanf("%lld",&n)) { memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%lld",&dis[i][j]); for(i=1;i<=n;i++) scanf("%lld",&d[i]); for(i=n;i>=1;i--)//倒叙计算,三重for循环,floyd算法核心内容。 { vis[d[i]]=1;//标记删除的点。 for(int j=1;j<=n;j++) for(int k=1;k<=n;k++) { dis[j][k]=min(dis[j][k],dis[j][d[i]]+dis[d[i]][k]);///放在外面 不断更新局部最优解。 if(vis[j]&&vis[k])ans[i]+=dis[j][k];//如果点被标记过,计算和。 } } for(i=1;i<=n;i++) if(i==n) printf("%lld\n",ans[i]); else printf("%lld ",ans[i]); } return 0; } /** * From: * Qingdao Agricultural University * Created by XiangwangAcmer * Date : 2019-10-03-09.33.52 * Talk is cheap.Show me your code. */ #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<cstdlib> #include<queue> #include<cmath> #include<cctype> #include<stack> #include<map> #include<string> #include<cstdlib> #define ll long long using namespace std; const ll maxn = 1e6 + 5; const ll minn = 1e9 + 5; const ll mod = 1000000007; const int INF = 0x3f3f3f3f; const long long LIMIT = 4294967295LL; const int N = 101; //vector<int>v[maxn]; int dp[maxn]; ll g[501][501]; bool row[maxn], col[maxn]; bool flag = 0; ll ans[maxn]; int vis[maxn], book[maxn]; queue<int>q;///同dijk实现一样 可以解决负权问题 void floyd(int n) { for(int k = n; k >= 1; k--) { book[vis[k]] = 1; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) { g[i][j] = min(g[i][j], g[i][vis[k]] + g[vis[k]][j]);///不断更新局部最优解,等到yeah,maybe i can wait someday which is used. if(book[i] && book[j]) { ans[k] += g[i][j]; } } } } int main() { ios::sync_with_stdio(false); memset(g, 0x3f, sizeof(g)); int n; cin>>n; for(int i = 1; i <= n; i++) g[i][i] = 0; ///初始化 int u, v, w; for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> g[i][j]; int index; for(int i = 1; i <= n; i++) { cin >> index; vis[i] = index; } floyd(n); for(int i = 1; i <= n; i++) cout << ans[i] << ' '; return 0; }