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. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them — for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.
Input
The first line contains integer n (2 ≤ n ≤ 300) — amount of cities in Berland. Then there follow n lines with n integer numbers each — the matrix of shortest distances. j-th integer in the i-th row — di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.
Next line contains integer k (1 ≤ k ≤ 300) — amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai and bi — pair of cities, which the road connects, ci — the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.
Output
Output k space-separated integers qi (1 ≤ i ≤ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.
Examples
2
0 5
5 0
1
1 2 3
3
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
17 12 思路:要求每一对的最短路,直接想到floyd,但是每次都全跑一次一定会超时,那就每次把修改的边作为中间点进行floyd即可,代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL; const int maxm = ; int G[maxm][maxm];
int N, K; void calculate() {
for(int k = ; k <= N; ++k)
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j)
G[i][j] = min(G[i][j], G[i][k] + G[k][j]);
} int main() {
scanf("%d", &N);
int t;
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j) {
scanf("%d", &G[i][j]);
}
calculate();
scanf("%d", &K);
int u, v;
for(int f = ; f < K; ++f) {
scanf("%d%d%d", &u, &v, &t);
LL ans = ;
if(t < G[u][v]) {
G[u][v] = G[v][u] = t;
for(int i = ; i <= N; ++i)
for(int j = ; j <= N; ++j) {
G[i][j] = min(G[i][j], min(G[i][u]+G[u][j], G[i][v]+G[v][j]));
G[j][i] = G[i][j];
}
}
for(int i = ; i < N; ++i)
for(int j = i+; j <= N; ++j)
ans += G[i][j];
printf("%I64d ", ans);
}
return ;
}
Day4 - M - Roads in Berland CodeForces - 25C的更多相关文章
- 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 ...
- CodeForces 25C(Floyed 最短路)
F - Roads in Berland Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
- Roads in Berland(图论)
Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-wa ...
- C. Roads in Berland
题目链接: http://codeforces.com/problemset/problem/25/C 题意: 给一个最初的所有点与点之间的最短距离的矩阵.然后向图里加边,原有的边不变,问加边后的各个 ...
- Chemistry in Berland CodeForces - 846E
题目 题意: 有n种化学物质,第i种物质现有bi千克,需要ai千克.有n-1种,编号为2-n的转换方式,每种都为(x,k),第i行是编号为i+1的转换方式,编号为i的转换方式(xi,ki)表示ki千克 ...
- 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 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- 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 ...
随机推荐
- iOS一个简单的设置圆角不引起性能问题的分类
http://www.cocoachina.com/articles/18756 iOS设置圆角矩形和阴影效果 https://www.cnblogs.com/rayshen/p/4900336.ht ...
- Ubuntu开启端口(持久化)
1.查看已经开启的端口 sudo ufw status 2.打开80端口 sudo ufw allow 3.防火墙开启 sudo ufw enable 4.防火墙重启 sudo ufw reload
- windows系统下hosts文件的改写(为了测试nginx内网的证书代理,需要做域名解析)
1. win加R C:\WINDOWS\system32\drivers\etc 2.打开hosts文件 加入一行 IP为客户机要访问的IP地址 域名也是在nginx中定义好的 3.ct ...
- [转]使用Struts 2防止表单重复提交
转自 用户重复提交表单在某些场合将会造成非常严重的后果.例如,在使用信用卡进行在线支付的时候,如果服务器的响应速度太慢,用户有可能会多次点击提交按钮,而这可能导致那张信用卡上的金额被消费了多次.因此, ...
- svn 回退/更新/取消某个版本命令详解
1,取消文件: svn revert 文件名 2,取消目录 svn revert --depth=infinity 目录名 3,回退版本 方法1: 用svn merge 1) 先svn up,保证更新 ...
- spring配置文件1
applicationContext.xml: <?xml version="1.0" encoding="UTF-8"?><beans xm ...
- AI基础概念
基础概念 epoch:使用训练的全部数据对模型进行一次完整的训练,被成为“一代训练”.当一个完整的数据集通过了神经网络一次并且返回了一次,这个过程称为一次epoch.(也就是说,所有训练样本在神经网络 ...
- c++构造函数的初始化列表(翁恺c++公开课[13])
初始化列表形式: class Point { private: const float x,y; Point(float xa = 0.0, flato ya = 0.0):y(ya),x(xa) { ...
- Linux创建智能DNS
根据客户端源IP地址的不同,DNS服务提供不同的解析地址 1.安装dns服务,修改全局配置文件/etc/named.conf # yum -y install bind # vim /etc/name ...
- alibaba sentinel限流组件 源码分析
如何使用? maven引入: <dependency> <groupId>com.alibaba.csp</groupId> <artifactId>s ...