题目链接:

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

题意:

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

思路:

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

代码:

  1. #include<bits/stdc++.h>
  2. #define LL long long
  3.  
  4. using namespace std;
  5. const int maxn=;
  6. int dist[maxn][maxn];
  7. LL ans;
  8. void modify(int u,int v,int w)
  9. {
  10. if(dist[u][v]>w)
  11. {
  12. ans-=(dist[v][u]-w);
  13. dist[u][v]=w;
  14. }
  15. return;
  16. }
  17. int main()
  18. {
  19. int n;
  20. cin>>n;
  21. for(int i=;i<=n;i++)
  22. {
  23. for(int j=;j<=n;j++)
  24. {
  25. cin>>dist[i][j];
  26. if(j>i)
  27. {
  28. ans+=dist[i][j];
  29. }
  30. }
  31.  
  32. }
  33. //cout<<ans<<endl;
  34. int k;
  35. cin>>k;
  36. for(int i=;i<=k;i++)
  37. {
  38. int u,v,w;
  39. cin>>u>>v>>w;
  40. modify(u,v,w);
  41. if(dist[u][v]!=dist[v][u])
  42. {
  43. dist[v][u]=dist[u][v];
  44. }
  45. //cout<<ans<<endl;
  46. for(int p=;p<=n;p++)
  47. {
  48. for(int q=;q<=n;q++)
  49. {
  50. modify(p,q,dist[p][u]+dist[v][q]+w);
  51. if(dist[p][q]!=dist[q][p])
  52. {
  53. dist[q][p]=dist[p][u]+dist[v][q]+w;
  54. }
  55. }
  56. }
  57. cout<<ans<<endl;
  58. }
  59.  
  60. return ;
  61. }

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. WOJ#4709 迷路

    WOJ#4709 迷路 题目描述 dolls意外得到了一张藏宝图,于是他踏上了寻找宝藏的道路.在走了许多许多步,回到同一个位置以后,dolls确定自己迷路了.dolls十分生气,他觉得自己这么英明圣武 ...

  2. 牛客练习赛51 C 勾股定理https://ac.nowcoder.com/acm/contest/1083/C

    题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角三角形. 输入描述: 一个整数n. 输出描述: 另外两条边b,c.答案不唯一,只要输出任意一组即为合理, ...

  3. HDU2188选拔自愿者

    悼念512汶川大地震遇难同胞--选拔志愿者 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. HDU 2783 You’ll be Working on the Railroad(最短路)

    You’ll be Working on the Railroad Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  5. Python时间模块datetime用法

    时间模块datetime是python内置模块,datetime是Python处理日期和时间的标准库. 1,导入时间模块 from datetime import datetime 2,实例 from ...

  6. win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据(使用外部redis)

    目录 话题 (191) 笔记 (137) 资料区 (2) 评价 (33) 介绍 讨论区 话题 win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据( ...

  7. MVC一个action对应多个视图的写法

    一,如下代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  8. TP中如何去掉index.php

    使用过TP的同学都知道,在URL始终会有index .php  我们如何才能够去掉呢? 1. 确认httpd.conf配置文件中加载了mod_rewrite.so模块 2. AllowOverride ...

  9. mybatis resultMap之collection聚集两种实现方式

    最近做得项目用到了MyBatis处理一对多的映射关系,下面的两个方法中用到了集合的嵌套查询方法,下面仔细学习一下这两种方式 聚集元素用来处理"一对多"的关系.需要指定映射的Java ...

  10. Maven生成可以直接运行的jar包的多种方式(转)

    转自:https://blog.csdn.net/xiao__gui/article/details/47341385 Maven可以使用mvn package指令对项目进行打包,如果使用java - ...