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 ...
随机推荐
- 九 三种Struts2访问Servlet方式总结
Servlet是单例的,Action是多例的. 多个程序访问Servlet只会创建一个Servlet对象,多个程序访问Action会创建对应的多个Action对象. 跳转页面可以获取对象的属性,说明使 ...
- ehcache配置文件
ehcache.xml: <?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi=& ...
- CODE 大全网站整站源码分享(带数据库)
CODE 大全是一个偏向于 JavaEE.JavaWeb,WEB 前端,HTML5,数据库,系统运维,编程技术开发的纯个人学习.交流性质的技术博客,一个很不错的网站,现在我免费分享给大家.对 java ...
- Vue入口页
Template里面的App就是在这个实例里面注册的App组件 也就是整个过程就是将el所标识的元素替换成<App/> 而App就是在此实例注册的App组件.
- Systemverilog for design 笔记(七)
转载请标明出处 第一章 接口(interface) 1.1. 接口的概念 接口允许许多信号合成一组由一个端口表示. 1.2. 接口声明 //接口定义 Interface main_bus ...
- Django 学习 之ORM多表操作
一.创建模型 1.模型关系整理 创建一对一的关系:OneToOne("要绑定关系的表名") 创建一对多的关系:ForeignKey("要绑定关系的表名") 创建 ...
- kubernetes 1.5.2 部署kube-dns 踩过的坑
看了kubernetes 权威指南 遇见了dns这一块.于是便按照书上的方式部署了一下. 书上使用的方式是:kube2sky+etcd+skydns的方式.按照书上的yaml写了一遍,发现无论如何都无 ...
- ES6之新的数据结构
Set Set 类似于数组,是一种集合的数据结构,和 Array 之间最大的区别是: Set中所有的成员都是唯一的. 可以把Set想象成是一个: 既没有重复元素,也没有顺序概念的数组. Set 本身是 ...
- C. Magic Grid 构造矩阵
C. Magic Grid time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【剑指Offer面试编程题】题目1352:和为S的两个数字--九度OJ
题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输入: 每个测试案例包括两行: 第一行包含一个整数n和k, ...