题目链接:

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. expdp和impdp的应用-高版本通过dblink导入到低版本

    今天接到要进行数据库用户的部分数据迁移需求,需求如下 IMP.WO.INSA开头的表只要结构,不要数据 B.TEMP.TMP开头的表不用导 其他表需要导出数据和表结构,同时要求导出此用户下的所有其他对 ...

  2. 15、前端知识点--MVVM

    MVVM模式的理解 MVVM模式里面,核心是数据. 各种前端框架,最核心的说就是保持了数据与视图的同步. 数据驱动思想:数据驱动视图. Vue不建议手动操作DOM. 以前是指令操作DOM,其实本质上底 ...

  3. Dorado环境启动出错Spring加载不到资源Bean配置 at org.springframework.asm.ClassReader.<init>(Unknown Source)

    ERROR: org.springframework.web.context.ContextLoader - Context initialization failed org.springframe ...

  4. Docker设置容器开机自启动

    设置如下: docker update --restart=always 镜像ID 例如:docker update --restart=always e39a959d7bff. 参考:https:/ ...

  5. 树——sum-root-to-leaf-numbers(根到叶节点数字之和)

    问题: Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a numb ...

  6. Ubuntu环境下SSH服务安装、SSH远程登录以及SSH数据传输

    https://www.cnblogs.com/asyang1/p/9467646.html SSH 为 Secure Shell 的缩写,为建立在应用层基础上的安全通信协议. 一.检查SSH服务是否 ...

  7. php内置函数分析之strrev()

    PHP_FUNCTION(strrev) { zend_string *str; char *e, *p; zend_string *n; if (zend_parse_parameters(ZEND ...

  8. SpringBoot---事务支持

    1.自动配置的事务管理器 1.1.使用JDBC  作为 数据访问技术  时,SpringBoot  为我们  定义了  PlatformTransactionManager的实现  DataSourc ...

  9. sklearn.metrics.mean_absolute_error

    注意多维数组 MAE 的计算方法 * >>> from sklearn.metrics import mean_absolute_error >>> y_true ...

  10. VR和AR

    VR 虚拟现实,比如你天气很炎热,戴上VR眼镜,出现在你眼前的是哈尔滨的冰雪世界 AR 增强现实,比如你站在一片碧绿的草地上,给你身边来一匹快马 想想一个单身狗去电影院,给你身边来一个美女作陪,这是多 ...