C. Roads in Berland
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 thatdi, 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.

Sample test(s)
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 

题目大意:给出一个矩阵,表示一个图和边权值(这里的边权值已经是两点间的最短距离)。然后给出K次修改,对于每一次边权修改,输出修改后任意两点的最小距离的和。

Floyd变形。首先任意两点的最小距离的和只能暴力求解,对于边的更新,我们肯定会想到跑一边floyd但这会超时。

对于第修改的边a,b我们可以知道任意两点i,j的最短距离要么经过a-b,要么经过b-a,要么不经过a点也不经过b点。比n^3要好一些.

dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][a]+dis[b][j]+c);
dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][b]+dis[a][j]+c);
/* ***********************************************
Author :pk29
Created Time :2015/8/23 8:53:43
File Name :4.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std; bool cmp(int a,int b){
return a>b;
}
int dis[][];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,k;
cin>>n;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
scanf("%d",&dis[i][j]);
}
cin>>k;
int a,b,c;
while(k--){
scanf("%d%d%d",&a,&b,&c);
if(c<dis[a][b]){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][a]+dis[b][j]+c);
dis[i][j]=dis[j][i]=min(dis[i][j],dis[i][b]+dis[a][j]+c);
}
}
ll sum=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
sum+=dis[i][j];
}
cout<<sum<<" ";
}
return ;
}
 

Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland的更多相关文章

  1. Codeforces Beta Round #25 (Div. 2 Only)D. Roads not only in Berland

    D. Roads not only in Berland time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. Codeforces Beta Round #25 (Div. 2 Only)

    Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...

  3. codeforces水题100道 第十七题 Codeforces Beta Round #25 (Div. 2 Only) A. IQ test (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/25/A题意:在n个书中找到唯一一个奇偶性和其他n-1个数不同的数.C++代码: #include ...

  4. Codeforces Beta Round #25 (Div. 2)--A. IQ test

    IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  5. Codeforces Beta Round #25 (Div. 2 Only) A. IQ test【双标记/求给定数中唯一的奇数或偶数】

    A. IQ test time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  6. Codeforces Beta Round #25 (Div. 2 Only)E. Test

    E. Test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  7. Codeforces Beta Round #89 (Div. 2) E. Bertown roads(Tarjan、边双连通分量)

    题目链接:http://codeforces.com/problemset/problem/118/E 思路:首先要判断图是否是边双连通,这个Tarjan算法可以判断,若low[v] > dfn ...

  8. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  9. Codeforces Beta Round #49 (Div. 2)

    Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...

随机推荐

  1. 算法复习——LCT(bzoj2049洞穴勘测)

    题目: Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连 ...

  2. 【2018.10.15】noip模拟赛Day1

    题面 wzj的题解 T1 随便搜 #include<bits/stdc++.h> #define ll long long using namespace std; inline int ...

  3. 转载:shell中awk printf的用法

    转载:http://www.linuxawk.com/jiaocheng/83.html 6. printf函数   打印输出时,可能需要指定字段间的空格数,从而把列排整齐.在print函数中使用制表 ...

  4. Bzoj2007 [Noi2010]海拔(平面图最短路)

    2007: [Noi2010]海拔 Time Limit: 20 Sec  Memory Limit: 552 MBSubmit: 2742  Solved: 1318[Submit][Status] ...

  5. POJ 3585 Accumulation Degree

    二次扫描与换根法 用于解决无根树,对于每一个节点作为根时都要统计 做法: 1.先以任意一个节点为根,做树形DP,保存每个节点的DP值 2.然后自上而下dfs,对于每个节点考虑以他为根的最大值 #inc ...

  6. [OS X实用技巧]机器人应用:一键将图片转换为PNG/JPEG/TIFF

    转自:http://www.maczhi.com/archives/2842.html 按教程老出错....试验了后使用: - 取得指定Finder对象,其它不变,但运行后不会出错. OS X实用技巧 ...

  7. KJ面试

    1.css input checkbox和radio样式美化 <span class="pay_list_c1 on"> <input type="ra ...

  8. Unable to locate Attribute with the the given name [] on this ManagedType

    最近在写Springboot+hibernate的项目,遇到这个问题 好坑,查了半天没发现我哪里配置错了,后来发现实体类声明字段大小写敏感,把声明字段改成小写就行了

  9. pandaboard安装ubuntu14.04系统遇到的问题

    按照该网址步骤安装https://www.eewiki.net/display/linuxonarm/PandaBoard 在linux kernel的./build_kernel.sh时,自动安装交 ...

  10. 一道题目- Find the smallest range that includes at least one number from each of the k lists

    You have k lists of sorted integers. Find the smallest range that includes at least one number from ...