CodeForces
来源:互联网 发布:苹果一体机数据恢复 编辑:程序博客网 时间:2024/06/05 13:18
Greg and Graph
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 and u 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
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 lineaij(1 ≤ aij ≤ 105, aii = 0) represents the weight of the edge that goes from vertex i to vertexj.
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 inC++. It is preferred to use the cin, cout streams of the %I64d specifier.
Example
Input
101
0
Input20 54 01 2Output9 0Input40 3 1 16 0 400 12 4 0 11 1 1 04 1 2 3Output17 23 404 0题意:有一张有向图,Greg玩一个游戏,游戏有n步,每步是删除一个点,包括和这个点相连的所有边,求出每删除一个点之前的任意两个点的最短路的和思路:求这种任意两点之间的最短路,一般都会想到Floyd-Warshall算法,关键是怎么用,这个算法的核心就是那个三重循环,最外层那个循环每次增加一个点,然后进行松弛操作,第一次是是否能通过1这个点进行松弛,第二次是是否通过1,2这两个点进行松弛,有点动态规划的意思,那么对于这个题我们可以用一个逆向思维,我们从最后一个一个的加点,这就和Fld算法一样了,只是在求最短距离和的时候我们只加那些已被我们加入的点的最短和#include<cstdio>#include<algorithm>#include<iostream>using namespace std;typedef long long ll;const ll INF=0x3f3f3f3f;const int maxn=505;ll e[maxn][maxn],ans[maxn];int a[maxn];bool vis[maxn];int n;ll sum;void init(){for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){if(i==j) e[i][j]=0; else e[i][j]=INF; }}int main(void){scanf("%d",&n);init();for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&e[i][j]);for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int k=n;k>=1;k--){sum=0; vis[a[k]]=true; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) e[i][j]=min(e[i][j],e[i][a[k]]+e[a[k]][j]);for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(e[i][j]!=INF&&vis[i]&&vis[j]) sum+=e[i][j];ans[k]=sum; } for(int i=1;i<=n;i++) cout<<ans[i]<<" ";cout<<endl;return 0; }
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- ZCMU 1486: 掰巧克力(签到题) <水坑水坑!!!!>
- imu误差的效果
- Java基础12 类型转换与多态
- Opencv常见错误整理
- web.xml配置servlet
- CodeForces
- 三、宇宙中的环境因素
- java面试题博客地址
- KMP算法,关键在于next表的构建
- Unity3D利用Photon实现实时联网对战(二)PUN SDK介绍
- .脏读,不可重复读,幻象读 丢失更新(乐观锁,悲观锁)
- tensorflow学习资源
- 【算法】反转字符串
- 如何查看SCI期刊影响因子