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

题意:给出任意两点之间的距离,然后逐个删除这些点和与点相连的边,问,在每次删除前的所有点对的最短距离之和

分析:首先想到的是floyd,但是如果从前往后处理,复杂度是(500)^4,超时,我们从后往前处理,这样我们可以看作是添加点,而且这样的话每次只需要考虑添加点的缩进,所以复杂度是(500)^3,注意,我们每次添加一个点,就给他一个标记,代表这个点已经添加,然后算距离的时候,只有添加过的点才能加上距离

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=500+10;
ll ma[maxn][maxn],num[maxn],ans[maxn];
int n,vis[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>ma[i][j];
    for(int i=1;i<=n;i++)
        cin>>num[i];
    for(int i=n;i>=1;i--)
    {
        int v=num[i];
        vis[v]=1;
        for(int j=1;j<=n;j++)
        for(int k=1;k<=n;k++)
            ma[j][k]=min(ma[j][k],ma[j][v]+ma[v][k]);
        for(int j=1;j<=n;j++)
            for(int k=1;k<=n;k++)
                if(vis[j]&&vis[k]&&j!=k)ans[i]+=ma[j][k];
    }
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<" ";
    cout<<endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/carcar/p/9939576.html

时间: 2024-08-03 14:16:10

295B - Greg and Graph (floyd逆序处理)的相关文章

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 295B B. Greg and Graph(floyd+dp)

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

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<li

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.

(单链表)单链表的整体逆序和局部逆序

题目一:将单链表翻转. 思路:有三种方式. 一:用数组存储单链表的值,然后重新逆序赋值,效率较低. 二:利用三个指针,在原来的基础上进行逆序.这种方法比较实用,效率也高. 三:从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,最后将第一个节点挪到新表的表尾.需要新建一个链表,这种方法和第二种差不多. 这里我就写出第二种方法,比较实用. 代码(方法二): struct ListNode { int val; ListNode *next; ListNode(int x) :

嵌入式 linux和c 指针顺序和逆序存储

顺序存储 #include<stdio.h>int main(){ char a[10]="1234567890"; char b[9]; char *p=a; char *q=b; int i=10; while(i--) *q++=*p++; printf("%s\n",b); return 0;} 逆序存储 #include<stdio.h>int main(){ char a[10]="1234567890"; c

leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ le

逆序输出一句话

对一句话里面的单词进行reverse,标点符号位置不变.符号包括:,.!? 例:输入:Hello, my dear friend! 输出:olleH, ym raed dneirf! 思路:由于需要逆序输出,因此想到利用栈数据结构来实现. 代码如下(调试环境:VS2013): 逆序输出一句话,布布扣,bubuko.com

1111 多个整数的逆序输出

题目来源:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=1111Description输入n和n个整数,以与输入顺序相反的顺序输出这n个整数.要求不使用数组,而使用递归函数实现.递归函数实现过程如下:void inverse(int n){    if(n >1)   {        (1) 读入一个整数,存入num;        (2)  将后面的n-1个数逆序输出: inverse(n-1);          (3)  输出num: