题目链接:

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. POJ 1383题解(树的直径)(BFS)

    题面 Labyrinth Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 4997 Accepted: 1861 Descript ...

  2. mysql-介绍、下载安装以及软件基本管理

    一.mysql介绍 mysql是一个关系型数据库管理系统,它是一个基于socket编写的C/S架构的软件. 客户端软件: mysql自带:如mysql命令,mysqldump命令等. python模块 ...

  3. Linux 修改hostname几种方式

    1:  hostname DB-Server          --运行后立即生效(新会话生效),但是在系统重启后会丢失所做的修改 2:  echo DB-Server  > /proc/sys ...

  4. 让Elasticsearch飞起来!——性能优化实践干货

    原文:让Elasticsearch飞起来!--性能优化实践干货 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...

  5. k3 cloud成本调整单

    做了成本调整单中的入库调整单或者出库调整单,进行入库成本核算和出库成本核算,做了入库调整单后在存货收发汇总表(按日期报表)中的收入部分会展示出来: 如果做的是期末余额成本调整,核算时会先删除手工新增的 ...

  6. installsheild2011打包程序internal build error 6213

    今天打包一个安装程序,总是出现报错,internal build error -6213,然后搜遍都没有找到什么解决方案.看到一个帖子,说是因为installsheild里面的build的时候自动扫描 ...

  7. JVM垃圾回收算法图解

    参考文档:https://www.toutiao.com/a6691966641242112516/ 垃圾搜集算法 标记-清除法 标记-清除算法采用从根集合(GC Roots)进行扫描,对存活的对象进 ...

  8. Linux就该这么学04学习笔记

    今天开始学习,开始做笔记,希望自己能坚持下去 参考链接:https://www.linuxprobe.com/chapter-04.html vim编辑器 Linux系统中通用的文本编辑器 vi的升级 ...

  9. nginx安装配置_runoob_阅读笔记_20190917

    Nginx 安装配置_runoob菜鸟教程 Nginx 安装配置 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向 ...

  10. django之子应用中开发视图函数

    一:修改视图函数 报错:ImportError: No module named 'django' 原因是:pycharm中的解释器未选择虚拟环境里面的python3 from django.shor ...