codeforces 295B B. Greg and Graph(floyd+dp)
来源:互联网 发布:全球癌症数据统计 编辑:程序博客网 时间:2024/05/07 21:24
题目链接:
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 507using 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 (""); }}
0 0
- codeforces 295B B. Greg and Graph(floyd+dp)
- 【codeforces 295B】Greg and Graph ( floyd )
- [Codeforces 295B]Greg and Graph(Floyd)
- CodeForces 295B - Greg and Graph Floyd的巧妙应用
- 【CodeForces】295B Greg and Graph 离线算法 Floyd
- CodeForces - 295B D - Greg and Graph(floyd算法)
- [CF]295B. Greg and Graph | Floyd
- codeforces 295B Greg and Graph
- Codeforces 295B Greg and Graph
- codeforces 295B Greg and Graph [Floyed]
- Codeforces 296D/295B(Greg and Graph)
- Codeforces 295 B Greg and Graph (Floyd_Warshall的深入理解)
- 那些年我们写过的三重循环----CodeForces 295B Greg and Graph 重温Floyd算法
- Cf #179 (Div. 1) B. Greg and Graph 活用三重floyd
- Codeforces 296D Greg And Graph (逆向处理)Floyd
- Greg and Graph+floyd算法的应用
- CF295B Greg and Graph(离线+Floyd)
- Codeforces 295B floyd
- Flask 中的路由与反向路由
- 可编辑的div,focus之后,光标怎么定位到文本最后
- iOS每日一记之——————iOS9系统策略更新
- C/C++ 笔试、面试题目大汇总
- linux 安装jdk
- codeforces 295B B. Greg and Graph(floyd+dp)
- iOS9升级后退回HTTP协议
- kafka+storm整合 java示例
- js中的函数
- I need one Texturepacker license
- memcached
- snmptrap 命令解析
- 好用的javascript插件集合
- jsp简单的用户登录界面