CF 179(div2)D(floyed)
来源:互联网 发布:axure7.0 mac 中文版 编辑:程序博客网 时间:2024/05/18 01:58
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 numberxi 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 thatd(i, v, u) is the shortest path between verticesv andu in the graph that formed before deleting vertexxi, then Greg wants to know the value of the following sum:.
Help Greg, print the value of the required sum before each step.
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 lineaij(1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertexi to vertexj.
The next line contains n distinct integers:x1, x2, ..., xn(1 ≤ xi ≤ n) — the vertices that Greg deletes.
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 inC++. It is preferred to use thecin,cout streams of the%I64d specifier.
101
0
20 54 01 2
9 0
40 3 1 16 0 400 12 4 0 11 1 1 04 1 2 3
dp[i][j][k] 表示i到j的只经过前k个点做中间节点的最短距离,则第k个点,要么用上,或者没用上
所以 dp[i][j][k] = min(dp[i][j][k-1],dp[i][k][k]+dp[k][j][k]);
再用滚动数组把状态压到二维,k放在最外层的循环。
而这道题是逐渐删点,可以到过来求,就是逐渐加点的过程,用一个vis标记当前点是否加入
然后每新加入一个点,就相当于原来floyed里的k加了一个,这样总的算法就还是O(n^3)
注意新进来一个点,必须先更新以这个点为首尾点到其他点的最短距离,然后再更新任意两点以这个点为中间节点的最短距离
详见代码
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define LL long longusing namespace std;const int maxn = 500 + 5;LL M[maxn][maxn];int vis[maxn],x[maxn];LL ans[maxn];int main(){ int n; while(scanf("%d",&n) != EOF){ memset(vis,0,sizeof(vis)); for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) cin >> M[i][j]; for(int i = 0;i < n;i++){ cin >> x[i]; } for(int u = n-1;u >= 0;u--){ vis[x[u]] = 1; int k = x[u]; LL sum = 0; //先更新新加入的点到其他点的最短距离 for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ if(vis[i] && vis[j]){ M[k][i] = min(M[k][i],M[k][j]+M[j][i]); M[i][k] = min(M[i][k],M[i][j]+M[j][k]); } } } //再更新其他任意两点间,以新加入点为中间点的的最短距离 for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) if(vis[i] && vis[j]) M[i][j] = min(M[i][j],M[i][k]+M[k][j]); //最后累加 for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) if(vis[i] && vis[j]) sum += M[i][j]; ans[u] = sum; } for(int i = 0;i < n-1;i++) cout << ans[i] << ' '; cout << ans[n-1] << endl; } return 0;}
- CF 179(div2)D(floyed)
- CF 103div2 D(dijkstra)
- CF 183(div2)
- CF 162(div2)
- cf div2 #444 (ABC)(D已补)
- CF 255 DIV2 D DZY Loves Modification(优先队列)
- CF 259div2 D (状态压缩dp)
- CF #302 Div2 D Destroying Roads(最短路)
- cf div2 #333 D. Lipshitz Sequence (单调栈)
- CF Round#424(div2)D题 二分+贪心
- CF #425 DIV2 D. Misha, Grisha and Underground(LCA)
- CF 174(div2) D
- CF 161 div2 D
- CF 189 div2 D
- CF 159 div2 d
- cf.256.div2.D
- cf R327 div2 D
- CF 174(div2) C
- 【笔试or面试】操作系统的多道编程
- I2C原理总结
- 读书:《Android系统源代码情景分析》- 简述Zygote进程和System进程的启动
- 修改JBoss7的http端口
- MFC中全局变量的定义及使用
- CF 179(div2)D(floyed)
- 亲和串 +KMP
- 最小堆的调整、插入和删除
- 滑动窗口协议
- win7 virtualbox Ubuntu无法上网问题-------------------DNS没配置
- 华硕P8Z77 双显示器连接方法, 远程桌面双显示器的连接方法
- word中管理论文引用的参考文献
- new material
- MFC WebBrowser 隐藏3D边框和去除滚动条