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. 【CCF】通信网络 简单搜索

    去重!不然有环就直接挂掉了...0分 #include<iostream> #include<cstdio> #include<string> #include&l ...

  2. 【shell】shell编程(四)-循环语句

    上篇我们学习了shell中条件选择语句的用法.接下来本篇就来学习循环语句.在shell中,循环是通过for, while, until命令来实现的.下面就分别来看看吧. for for循环有两种形式: ...

  3. C++ assert 的一点说明

    断言(ASSERT)的用法 转载自http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数 ...

  4. SPOJ 1479 +SPOJ 666 无向树最小点覆盖 ,第二题要方案数,树形dp

    题意:求一颗无向树的最小点覆盖. 本来一看是最小点覆盖,直接一下敲了二分图求最小割,TLE. 树形DP,叫的这么玄乎,本来是线性DP是线上往前\后推,而树形DP就是在树上,由叶子结点状态向根状态推. ...

  5. C# 编程中的堆栈(Stack)和队列(Queue)

    一.什么是堆?(Heap)      堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收.      是程序运行期 ...

  6. (1)Swing创建窗体

    本系列使用Intellij IDEA 2017.3.4版本 一.运行窗体 1. 2. 3. 4. 5. 6. 给JPanel起个名字 -如From 7. 8. 9. 生成 import javax.s ...

  7. Spring Boot集成Spring Data Reids和Spring Session实现Session共享(多个不同的应用共用一个Redis实例)

    从Redis的Key入手,比如Spring Session在注解@EnableRedisHttpSession上提供了redisNamespace属性,只需要在这里设置不同的值即可,效果应该是这样的: ...

  8. 如何删除xcode启动主页面项目列表

    Open Xcode, leave the splash screen up and choose "File", "Open Recent Projects" ...

  9. android 获得屏幕宽度和高度

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  10. Android应用开发 WebView与服务器端的Js交互

    最近公司再添加功能的时候,有一部分功能是用的html,在一个浏览器或webview中展示出html即可.当然在这里我们当然用webview控件喽 WebApp的好处: 在应用里嵌套web的好处有这么几 ...