C. Roads in Berland
题目链接:
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的更多相关文章
- 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 ...
- Roads in Berland(图论)
Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-wa ...
- 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 ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- CF 191C Fools and Roads lca 或者 树链剖分
They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...
- 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 ...
- 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 ...
- CF191C Fools and Roads - 树剖解法
Codeforces Round #121 (Div. 1) C. Fools and Roads time limit per test :2 seconds memory limit per te ...
随机推荐
- WOJ#4709 迷路
WOJ#4709 迷路 题目描述 dolls意外得到了一张藏宝图,于是他踏上了寻找宝藏的道路.在走了许多许多步,回到同一个位置以后,dolls确定自己迷路了.dolls十分生气,他觉得自己这么英明圣武 ...
- 牛客练习赛51 C 勾股定理https://ac.nowcoder.com/acm/contest/1083/C
题目描述 给出直角三角形其中一条边的长度n,你的任务是构造剩下的两条边,使这三条边能构成一个直角三角形. 输入描述: 一个整数n. 输出描述: 另外两条边b,c.答案不唯一,只要输出任意一组即为合理, ...
- HDU2188选拔自愿者
悼念512汶川大地震遇难同胞--选拔志愿者 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 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 ...
- Python时间模块datetime用法
时间模块datetime是python内置模块,datetime是Python处理日期和时间的标准库. 1,导入时间模块 from datetime import datetime 2,实例 from ...
- win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据(使用外部redis)
目录 话题 (191) 笔记 (137) 资料区 (2) 评价 (33) 介绍 讨论区 话题 win10专业版Hyper-v下Docker挂载volume的方式使用Gitlab(汉化版)保存资料数据( ...
- MVC一个action对应多个视图的写法
一,如下代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- TP中如何去掉index.php
使用过TP的同学都知道,在URL始终会有index .php 我们如何才能够去掉呢? 1. 确认httpd.conf配置文件中加载了mod_rewrite.so模块 2. AllowOverride ...
- mybatis resultMap之collection聚集两种实现方式
最近做得项目用到了MyBatis处理一对多的映射关系,下面的两个方法中用到了集合的嵌套查询方法,下面仔细学习一下这两种方式 聚集元素用来处理"一对多"的关系.需要指定映射的Java ...
- Maven生成可以直接运行的jar包的多种方式(转)
转自:https://blog.csdn.net/xiao__gui/article/details/47341385 Maven可以使用mvn package指令对项目进行打包,如果使用java - ...