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 aibici (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

Input
2
0 5
5 0
1
1 2 3
Output
3 
Input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
Output
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的更多相关文章

  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. CodeForces 25C(Floyed 最短路)

    F - Roads in Berland Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I6 ...

  3. 【Codeforces 25C】Roads in Berland

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...

  4. Roads in Berland(图论)

    Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-wa ...

  5. C. Roads in Berland

    题目链接: http://codeforces.com/problemset/problem/25/C 题意: 给一个最初的所有点与点之间的最短距离的矩阵.然后向图里加边,原有的边不变,问加边后的各个 ...

  6. Chemistry in Berland CodeForces - 846E

    题目 题意: 有n种化学物质,第i种物质现有bi千克,需要ai千克.有n-1种,编号为2-n的转换方式,每种都为(x,k),第i行是编号为i+1的转换方式,编号为i的转换方式(xi,ki)表示ki千克 ...

  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 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  9. 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 ...

随机推荐

  1. 番外篇!全球首个微信应用号开发教程!小程序 DEMO 视频奉上!

    大家好,我是博卡君.经过国庆节的七天假期,相信很多朋友都已经研究出自己的小程序 demo 了吧?我最近也利用休息时间关注了一下网上关于小程序开发的讨论,今天就利用这个番外篇谈谈自己对小程序的一些想法吧 ...

  2. UITextField的快速基本使用代码块

    概述 UITextField在界面中显示可编辑文本区域的对象. 您可以使用文本字段来使用屏幕键盘从用户收集基于文本的输入.键盘可以配置许多不同类型的输入,如纯文本,电子邮件,数字等等.文本字段使用目标 ...

  3. IDEA快捷键/本文仅供自己参考使用如有侵权立删

    好好来学习学习IDEA这款神器,让你的效率飞起!视频来自慕课网 本文转载 更多技巧 代码定位 跳转: 1.IDEA的左侧侧边栏有1:Project.7:Structure和2:Favorities a ...

  4. P1017进制转化

    P1017进制转化 也不知道为啥,这么简单的题困扰了我这么长时间 #include<cstdio> using namespace std; int m; //被除数= 除数*商 + 余数 ...

  5. c++将字符转换成字符串

    转载:https://blog.csdn.net/dididisailor/article/details/83189135 char c; string str; stringstream stre ...

  6. java使用netty模拟实现一个类dubbo的分布式服务调用框架

    本文较长,如果想直接看代码可以查看项目源码地址: https://github.com/hetutu5238/rpc-demo.git 要想实现分布式服务调用框架,我们需要了解分布式服务一般需要的功能 ...

  7. Educational Codeforces Round 73 (Rated for Div. 2)E(思维,博弈)

    //这道题博弈的核心就是不能让后手有一段只能放b而长度不够放a的段,并且先手要放最后一次#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h> ...

  8. 敏感、脆弱,OLED柔性屏会带给制造业怎样的灾难?

    智能手机市场已经有些意兴阑珊,大家创新的点子在过去十年逐一亮剑,也带来了一些革命,现在谈到智能手机硬件,只能是更大的屏幕.更大的电池,更高的摄像头像素等等,长期的创新乏力,已经让消费者失去" ...

  9. 在win10中启动redis

    1.输入命令"redis-server.exe redis.windows.conf ",按回车键, 2.输入“redis-cli.exe -h 127.0.0.1 -p 6379 ...

  10. luogu P3356 火星探险问题

    本题很简单的费用流问题,有石头的点需要限制,那我们就可以拆点,capacity为1就可以限制,然后cost为-1,直接跑板子就可以了,注意输出的时候找残量网络的反向边