Codeforces 295B Greg and Graph

题意:给你一个完全图,给你删点顺序,问你这些点删除前 整个图的最短路的和。

解题思路:这其实是floyd-warshall 算法的 一个应用。我们可以从最后一个点开始 ,对他的顺序进行映射,题意就正好符合floyd 的定义。

解题代码:

 1 // File Name: 295b.cpp
 2 // Author: darkdream
 3 // Created Time: 2015年03月11日 星期三 19时13分13秒
 4
 5 #include<vector>
 6 #include<list>
 7 #include<map>
 8 #include<set>
 9 #include<deque>
10 #include<stack>
11 #include<bitset>
12 #include<algorithm>
13 #include<functional>
14 #include<numeric>
15 #include<utility>
16 #include<sstream>
17 #include<iostream>
18 #include<iomanip>
19 #include<cstdio>
20 #include<cmath>
21 #include<cstdlib>
22 #include<cstring>
23 #include<ctime>
24 #define LL long long
25
26 using namespace std;
27 LL dp[505][505];
28 int de[505];
29 int hs[505];
30 int tmp[505][505];
31 LL ans[505];
32 int main(){
33     int n ;
34     scanf("%d",&n);
35     for(int i = 1;i <= n;i ++)
36     {
37         for(int j= 1;j <= n ;j ++)
38             scanf("%d",&tmp[i][j]);
39     }
40     for(int i = 1;i <= n;i ++)
41         scanf("%d",&de[i]);
42     for(int i = n ;i >= 1;i --)
43     {
44        hs[n-i+1]  = de[i];
45     }
46     for(int i= 1;i <= n;i ++)
47     {
48       for(int j = 1;j <= n; j ++)
49       {
50         dp[i][j] = tmp[hs[i]][hs[j]];
51       }
52     }
53
54     for(int k = 1; k <= n;k ++)
55     {
56        for(int i = 1;i <= n;i ++)
57        {
58           for(int j = 1;j <= n;j ++)
59           {
60              dp[i][j] = min(dp[i][j],dp[i][k] + dp[k][j]);
61     //         printf("%I64d ",dp[i][j]);
62           }
63     //      printf("\n");
64        }
65        LL sum = 0 ;
66        for(int i = 1;i <= k; i ++)
67        {
68           for(int j = 1;j <= k;j ++)
69           {
70              sum += dp[i][j];
71           }
72        }
73        ans[k] = sum;
74     }
75     for(int i =n;i >=1; i --)
76     {
77        printf("%I64d ",ans[i]);
78     }
79     printf("\n");
80
81 return 0;
82 }

时间: 2024-11-05 21:59:54

Codeforces 295B Greg and Graph的相关文章

295B - Greg and Graph (floyd逆序处理)

题意:给出任意两点之间的距离,然后逐个删除这些点和与点相连的边,问,在每次删除前的所有点对的最短距离之和 分析:首先想到的是floyd,但是如果从前往后处理,复杂度是(500)^4,超时,我们从后往前处理,这样我们可以看作是添加点,而且这样的话每次只需要考虑添加点的缩进,所以复杂度是(500)^3,注意,我们每次添加一个点,就给他一个标记,代表这个点已经添加,然后算距离的时候,只有添加过的点才能加上距离 代码: #include <bits/stdc++.h> using namespace

codeforces 295B B. Greg and Graph(floyd+dp)

题目链接: codeforces 295B 题目大意: 给出n个点的完全有权有向图,每次删去一个点,求每次操作前整张图各个点的最短路之和. 题目分析: 首先删边对于我们来说是不好做的,所以我们想到了通过加点的方式逆向地做,那么加点怎么做呢? 其实就是一个我们很熟悉的算法:floyd,因为我们通常用的都是它的简化版本,所以可能很多人并不理解它的确切的思想. 在介绍这道题的具体解法之前,我先解释一下floyd,可能之后的问题就迎刃而解了. 首先floyd其实是动态规划,没有省略时的状态是这样定义的.

Problem B Codeforces 295B 最短路(floyd)

Description 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

Greg and Graph+floyd算法的应用

Greg and Graph time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3

CodeForces 295B

/*从要删除最后一个到第一个进行松弛操作, 因为如果从第一个到最后一个进行松弛操作,弟24行有可能不会更新,若要更新,就需要再一次对d[N][N]初始化*/ 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long ll; 5 #define N 505 6 //const int inf=1<<30; 7 ll d[N][N],dis[N]; 8 int d

Codeforces Round #179 (Div. 2)---D. Greg and Graph(离线+floyd)

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.

【CodeForces - 296D】Greg and Graph(floyd)

Description 题意:给定一个有向图,一共有N个点,给邻接矩阵.依次去掉N个节点,每一次去掉一个节点的同时,将其直接与当前节点相连的边和当前节点连出的边都需要去除,输出N个数,表示去掉当前节点之前的所有两点间最短距离和.n<=500 Solution 如果暴力打肯定是会超时的,那就要运用到floyd(hj) floyd算法内2个循环就相当于新加入外循环的那个点然后跟新最短路, 所以可以把题目看成倒过来依次加点,每次\(n^2\)平方更新一下,总共\(O(n^3)\) Code #incl

codeforces 340D Bubble Sort Graph(dp,LIS)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud  Bubble Sort Graph Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple al