Cf #179 (Div. 1) B. Greg and Graph 活用三重floyd
来源:互联网 发布:阿卡索真实评价知乎 编辑:程序博客网 时间:2024/05/29 11:45
分析:活用floyd的三重循环,降低复杂度,注意ans是求和,所以会爆int#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int mod=100000000;
ll max(ll a,ll b)
{return a>b?a:b;};
int min(int a,int b)
{return a<b?a:b;};
int dp[505][505],use[505],ope[505];
ll ans[505];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&dp[i][j]);
for(int i=1;i<=n;i++)
scanf("%d",&ope[i]);
MM(use,0);MM(ans,0);
for(int k=n;k>=1;k--)//每次加入一个节点,就以改点更新整个图,将暴力的n^4降到n^3
{
int cur=ope[k];
use[cur]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dp[i][j]=min(dp[i][j],dp[i][cur]+dp[cur][j]);//更新整个图
for(int i=1;i<=n;i++)
if(use[i])//如果已经加入该节点
for(int j=1;j<=n;j++)
if(use[j])
ans[k]+=dp[i][j];
}
for(int i=1;i<=n;i++) printf("%lld ",ans[i]);
printf("\n");
}
return 0;
}
- Cf #179 (Div. 1) B. Greg and Graph 活用三重floyd
- [CF]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 B. Greg and Graph(floyd+dp)
- CodeForces 295B - Greg and Graph Floyd的巧妙应用
- 【CodeForces】295B Greg and Graph 离线算法 Floyd
- CodeForces - 295B D - Greg and Graph(floyd算法)
- Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)
- Greg and Graph+floyd算法的应用
- CF295B Greg and Graph(离线+Floyd)
- codeforces 295B Greg and Graph
- Codeforces 295B Greg and Graph
- codeforces 295B Greg and Graph [Floyed]
- Codeforces 296D Greg And Graph (逆向处理)Floyd
- Codeforces 296D/295B(Greg and Graph)
- Codeforces 295 B Greg and Graph (Floyd_Warshall的深入理解)
- java插入排序
- 运算符的优先级
- yocto工程软件包编译依赖关系
- Good Bye 2015 B. 位运算
- C++算法之 求二叉树的节点个数、深度、四种遍历方法
- Cf #179 (Div. 1) B. Greg and Graph 活用三重floyd
- CF #165 DIV2 E 最大流的流向
- Manthan 2011 A. Partial Teacher 拓扑排序or直接数组处理
- 开源项目地址-apache
- apt命令与yum命令
- (四)IP协议--图解TCP/IP读书笔记
- 计算几何基础知识
- 存储过程in和out
- 深入理解mybatis原理(七) MyBatis的架构设计以及实例分析