题目链接:

http://codeforces.com/problemset/problem/25/C

题意:

给一个最初的所有点与点之间的最短距离的矩阵。然后向图里加边,原有的边不变,问加边后的各个顶点的距离是多少。

思路:

这个一看就知道是folyd的变种,关键是状态转移怎么处理,具体看代码。

代码:

#include<bits/stdc++.h>
#define LL long long using namespace std;
const int maxn=;
int dist[maxn][maxn];
LL ans;
void modify(int u,int v,int w)
{
if(dist[u][v]>w)
{
ans-=(dist[v][u]-w);
dist[u][v]=w;
}
return;
}
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>dist[i][j];
if(j>i)
{
ans+=dist[i][j];
}
} }
//cout<<ans<<endl;
int k;
cin>>k;
for(int i=;i<=k;i++)
{
int u,v,w;
cin>>u>>v>>w;
modify(u,v,w);
if(dist[u][v]!=dist[v][u])
{
dist[v][u]=dist[u][v];
}
//cout<<ans<<endl;
for(int p=;p<=n;p++)
{
for(int q=;q<=n;q++)
{
modify(p,q,dist[p][u]+dist[v][q]+w);
if(dist[p][q]!=dist[q][p])
{
dist[q][p]=dist[p][u]+dist[v][q]+w;
}
}
}
cout<<ans<<endl;
} return ;
}

C. Roads in Berland的更多相关文章

  1. Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland

    C. Roads in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. Roads in Berland(图论)

    Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-wa ...

  3. Day4 - M - Roads in Berland CodeForces - 25C

    There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac ...

  4. 【Codeforces 25C】Roads in Berland

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...

  5. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  6. CF 191C Fools and Roads lca 或者 树链剖分

    They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...

  7. Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路

    E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...

  8. codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)

    Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...

  9. CF191C Fools and Roads - 树剖解法

    Codeforces Round #121 (Div. 1) C. Fools and Roads time limit per test :2 seconds memory limit per te ...

随机推荐

  1. Scrapy框架的应用

    一, Scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化等)的具有 ...

  2. python里的排序

    本篇文章主要讲: 自定义规则排序 多字段排序 开讲之前,先讲一些简单sorted()或者sort(),两者返回值不同!大家自行学习,不是本文的重点! sorted([5, 2, 3, 1, 4]) # ...

  3. Leetcode Lect4 二叉树中的分治法与遍历法

    在这一章节的学习中,我们将要学习一个数据结构——二叉树(Binary Tree),和基于二叉树上的搜索算法. 在二叉树的搜索中,我们主要使用了分治法(Divide Conquer)来解决大部分的问题. ...

  4. 安装kali linux 后出现文字乱码问题

    在安装kali时我选择中文安装,结果安装完成后出现文字乱码现象 在经过上网查询后,采用了CSDN博客站中的 stubbornness1219 这位博主的解决方案成功将问题解决. 解决方案:终端下执行s ...

  5. WAF防火墙学习

    正则解析神器 http://rick.measham.id.au/paste/explain.pl http://regexr.com/ http://regex101.com/ http://www ...

  6. Codeforces Round #425 (Div. 2) - A

    题目链接:http://codeforces.com/contest/832/problem/A 题意:有n个棍子,两个人轮流取这些棍子,每个人每次只能去恰好k个棍子(不足k个则不能取),问先手取的棍 ...

  7. LightOJ 1289 LCM from 1 to n(位图标记+素数筛

    https://vjudge.net/contest/324284#problem/B 数学水题,其实就是想写下位图..和状压很像 题意:给n让求lcm(1,2,3,...,n),n<=1e8 ...

  8. mysql数据按条件导出

    仅导出部分数据: mysqldump -hlocalhost -uuser -p --skip-triggers --no-create-info dbname tbname -w "id ...

  9. web--响应式导航菜单

    响应式导航菜单 代码如下 HTML代码: <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  10. @ResponseBody和@RestController

    Spring 关于ResponseBody注解的作用 responseBody一般是作用在方法上的,加上该注解表示该方法的返回结果直接写到Http response Body中,常用在ajax异步请求 ...