Round 6 B - Greg and Graph CodeForces - 295Div.1B - Floyd && DP
2015-09-18 11:53
489 查看
codeforces 295B B. Greg and Graph(floyd+dp)
标签:codeforcesdpfloyd最短路
2015-09-18 11:53
1251人阅读 评论(1)
收藏
举报
分类:
*ACM解题报告*(670)
作者同类文章X
动态规划(205)
作者同类文章X
最短路--floyd(2)
作者同类文章X
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
题目链接
题目大意
题目分析
AC代码
题目链接:
codeforces 295B题目大意:
给出n个点的完全有权有向图,每次删去一个点,求每次操作前整张图各个点的最短路之和。题目分析:
首先删边对于我们来说是不好做的,所以我们想到了通过加点的方式逆向地做,那么加点怎么做呢?其实就是一个我们很熟悉的算法:floyd,因为我们通常用的都是它的简化版本,所以可能很多人并不理解它的确切的思想。
在介绍这道题的具体解法之前,我先解释一下floyd,可能之后的问题就迎刃而解了。
首先floyd其实是动态规划,没有省略时的状态是这样定义的。dp[k][i][j]代表前k个点作为媒介的情况下i到j的最短路。
转移方程也很简单,如下:
dp[k][i][j]=min(dp[k−1][i][j],dp[k−1][i][k]+dp[k−1][k][j])
这样我们就知道了n3的递推中,最后一维其实标记的是作为媒介的点是前k个的情况,那么也就是我们之要1~k这些点组成图的最短路,那么因为在求取最短路时,并没有用后面的点作为媒介,所以就是我们要的答案,因为加入点无序,所以我们要先用hash将点对应到有序的序列上,然后直接利用floyd做就可以了,不懂的可以在评论中询问,我会耐心解答的。
AC代码:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <algorithm> #include <map> #define MAX 507 using namespace std; typedef long long LL; LL dp[MAX][MAX],ans[MAX],a[MAX][MAX]; int n,x[MAX]; map<int,int> mp; int main ( ) { while (~scanf ( "%d" , &n )) { mp.clear(); for ( int i = 1 ; i <= n ; i++ ) for ( int j = 1 ; j <= n ; j++ ) scanf ( "%I64d" , &a[i][j] ); for ( int i = 1 ; i <= n ; i++ ) { scanf ( "%d" , &x[i] ); mp[x[i]] = n+1-i; } for ( int i = 1 ; i <= n ; i++ ) for ( int j = 1 ; j <= n ; j++ ) dp[mp[i]][mp[j]] = a[i][j]; for ( int k = 1; k <= n ; k++ ) { for ( int i = 1 ; i <= n ; i++ ) for ( int j = 1 ; j <= n ; j++ ) dp[i][j] = min ( dp[i][j] , dp[i][k] + dp[k][j] ); for ( int i = 1 ; i <= k; i++ ) for ( int j = 1 ; j <= k; j++ ) ans[n-k+1] += dp[i][j]; } for ( int i = 1 ; i <= n ; i++ ) printf ( "%I64d " , ans[i] ); puts (""); } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
顶 1 踩 0
上一篇codeforces 225C C. Barcode(dp)
下一篇codeforces 416C C. Booking System(贪心)
相关文章推荐
•
CodeForces 295A Greg and Array(线段树)
•
Codeforces 461B Appleman and Tree(树形dp)
•
CodeForces - 295B D - Greg and Graph(floyd算法)
•
Codeforces 437B The Child and Set(暴力)
•
Codeforces 295 B Greg and Graph (Floyd_Warshall的深入理解)
•
Codeforces 398B Painting The Wall(dp)
•
CodeForces 295B - Greg and Graph Floyd的巧妙应用
•
Codeforces 453B Little Pony and Harmony Chest(状压)
•
那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
•
Codeforces 459E Pashmak and Graph(dp+贪心)
相关文章推荐
- iOS每日一记之——————iOS9系统策略更新
- 可编辑的div,focus之后,光标怎么定位到文本最后
- IOS-手机来电或者第三方音频打断当前播放
- linux 安装jdk
- 浏览器的各种长度宽度clientX clientY pageX pageY x y
- codeforces 295B B. Greg and Graph(floyd+dp)
- OC基础语法
- 生产源库位和目的库位动态过滤
- 图片比例缩放控制
- HDU 5441.Travel【2015 ACM/ICPC Asia Regional Changchun Online】【并查集】9月18
- 查找算法的实现 顺序和折半
- Ubuntu中Git服务器搭建
- VC下动态库DLL、静态库LIB的编写和使用方法
- STM32 IAP 在线升级详解
- Apache 负载均衡 session
- IOS单例
- ASP.NET窗体身份验证详解
- apache URL 区别大小写
- linux安装java
- 死亡通知与进程异常退出;ANR与进程重启后状态跟之前不对