F - Roads in Berland

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u

Description

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 Input

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

这道题目对每次增加的路跑一下Floyed就好了,输出格式好像没有限制

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h> using namespace std;
int a[305][305];
int k;
int n;
int x,y,z;
int main()
{
scanf("%d",&n);
int sum=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{scanf("%d",&a[i][j]);sum+=a[i][j];}
sum/=2;
scanf("%d",&k);
long long int num;
for(int i=1;i<=k;i++)
{
scanf("%d%d%d",&x,&y,&z);
num=0;
if(a[x][y]>z)
{
// sum-=(a[x][y]-z);
a[x][y]=z;
a[y][x]=z;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{ if(a[i][j]>(a[i][x]+a[y][j]+a[x][y]))
{ //num=(a[i][j]-a[i][x]-a[y][j]-a[x][y]);
//sum-=(a[i][j]-a[i][x]-a[y][j]-a[x][y]);
a[i][j]=(a[i][x]+a[y][j]+a[x][y]);
//a[j][i]=(a[i][x]+a[y][j]+a[x][y]); }
if(a[i][j]>(a[i][y]+a[x][j]+a[x][y]))
{
//num=(a[i][j]-a[i][x]-a[y][j]-a[x][y]);
//sum-=(a[i][j]-a[i][y]-a[x][j]-a[x][y]);
a[i][j]=(a[i][y]+a[x][j]+a[x][y]);
//a[j][i]=(a[i][y]+a[x][j]+a[x][y]); }
num+=a[i][j]; }
}
printf("%lld ",num/2); }
printf("\n");
return 0;
}

CodeForces 25C(Floyed 最短路)的更多相关文章

  1. codeforces DIV2 D 最短路

    http://codeforces.com/contest/716/problem/D 题目大意:给你一些边,有权值,权值为0的表示目前该边不存在,但是可以把0修改成另外一个权值.现在,我们重新建路, ...

  2. [luogu2047 NOI2007] 社交网络 (floyed最短路)

    传送门 输入输出样例 输入样例#1: 4 4 1 2 1 2 3 1 3 4 1 4 1 1 输出样例#1: 1.000 1.000 1.000 1.000 题解 在进行floyed的过程中,顺便更新 ...

  3. Dynamic Shortest Path CodeForces - 843D (动态最短路)

    大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...

  4. 【Codeforces 25C】Roads in Berland

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

  5. 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. Eac ...

  6. World Tour CodeForces - 667D (bfs最短路)

    大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大

  7. Bakery CodeForces - 707B (最短路的思路题)

    Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...

  8. CodeForces 689B【最短路】

    题意: 给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1. 思路: 直接建边,跑spfa就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...

  9. Make It One CodeForces - 1043F (数论,最短路,好题)

    大意: 给定序列$a$, 求最小子集, 使得gcd为1. 对于数$x$, 素因子多少次幂是无关紧要的, 这样就可以用一个二进制数来表示. $x$取$gcd$后的二进制状态最多$2^7$, 可以暴力枚举 ...

随机推荐

  1. 【转】7Z命令行解压缩

    7z.exe在CMD窗口的使用说明如下: 7-Zip (A) 4.57 Copyright (c) 1999-2007 Igor Pavlov 2007-12-06 Usage: 7za <co ...

  2. e659. 缩放,剪取,移动,翻转图形

    AffineTransform tx = new AffineTransform(); tx.scale(scalex, scaley); tx.shear(shiftx, shifty); tx.t ...

  3. TinyOS节点间通信相关接口和组件介绍

    一.基本通信接口:   Packet:提供了对message_t抽象数据类型的基本访问.这个接口的命令有:清空消息内容,获得消息的有效载荷区长度,获得消息有效载荷区的指针. //tos/interfa ...

  4. tRNA 二级结构预测可视化

    tRNAdb 收录了来自104个物种的623条tRNA 序列,从数据库中下载对应物种的tRNA 序列和二级结构,以人为例 打开下面的链接 http://trna.bioinf.uni-leipzig. ...

  5. CentOS后台运行和关闭、查看后台任务命令

    fg.bg.jobs.&.nohup.ctrl+z.ctrl+c 命令 一.& 加在一个命令的最后,可以把这个命令放到后台执行,如 watch -n 10 sh test.sh &am ...

  6. 电视不支持AirPlay镜像怎么办?苹果iPhone手机投屏三种方法

    导读:苹果手机多屏互动功能在哪里?iPhone苹果手机没有AirPlay镜像怎么办?三种方法教你苹果iPhone手机怎么投影到智能电视上. 前言: 苹果iPhone手机投屏到电视设备上,需要使用到Ai ...

  7. C# CRC16 查表法

    private static ushort[] crctab = new ushort[256]{                      0x0000, 0x1021, 0x2042, 0x306 ...

  8. js 对象引用传值

    1)当变量是一个对象(例如:{}或[]),使用 a = b = {} 这种形式传值的话,会变成会用传值,修改 a 时 b 会相应变化,修改 b 时 a 也一样会相应变化 var a = b = {}; ...

  9. 【RF库Collections测试】combine lists

    Arguments: [ *lists ]Combines the given `lists` together and returns the result. The given lists are ...

  10. Linux alias 命令

    alias命令用于查看或设置命令别名,但仅作用于该次登陆的会话,若要永久使用别名,可在 ~/.bashrc 中设定别名 [root@localhost ~]$ alias // 查看别名 [root@ ...