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. 第三百零一节,python操作redis缓存-管道、发布订阅

    python操作redis缓存-管道.发布订阅 一.管道 redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作,如果想要在一次请求中指定多个命令,则可以使用pi ...

  2. Windows上建立、取消共享文件夹

    建立共享文件夹 1.创建一个文件夹test 2.右键属性,点击共享 4.在另外一台机器上访问该共享文件 取消共享文件夹 右键属性,点击高级共享

  3. 什么是Apache ZooKeeper?

    Apache ZooKeeper是由集群(节点组)使用的一种服务,用于在自身之间协调,并通过稳健的同步技术维护共享数据.ZooKeeper本身是一个分布式应用程序,为写入分布式应用程序提供服务. Zo ...

  4. C++ 语言中的重载、内联、缺省参数、隐式转换等机制展现了很多优点

    C++ 语言中的重载.内联.缺省参数.隐式转换等机制展现了很多优点,但是这些 优点的背后都隐藏着一些隐患.正如人们的饮食,少食和暴食都不可取,应当恰到好处. 我们要辨证地看待 C++的新机制,应该恰如 ...

  5. 关于Bundle

    1. 黄色的文件夹,打包的时候,不会建立目录,主要保存程序文件 - 素材不允许重名 2. 蓝色的文件夹,打包的时候,会建立目录,可以分目录的存储素材文件 - 素材可以重名 - 游戏的场景,backgr ...

  6. <UIKit>关于剪贴板共享数据

     http://blog.sina.com.cn/s/blog_45e2b66c010102h9.html 上面这篇文章将剪贴板的使用方法基本上已经讲清楚了,参考这篇文章,再加上一个使用剪贴板共享数据 ...

  7. C#中汉字轻松得到拼音全文类

    public class chs2py { ,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-, -,-,-,-,-,-,-,-,- ...

  8. linux vi编辑器中,如何通过快捷键上下翻页?

    需求说明: 之前在vi的时候,如果想看下一页,就直接按住 ↓ 这个箭头一直翻,现在觉得有些麻烦, 就找了下上,下翻页的快捷方式.在此记录下. 记录: 1.向下翻页快捷键(下一页):Ctrl + f 2 ...

  9. C#的字符串优化-String.Intern、IsInterned

    https://www.jianshu.com/p/af6eb8d3d4bf 首先看一段程序: using System; class Program { static void Main(strin ...

  10. mybatis由浅入深day02_7.4mybatis整合ehcache_7.5二级缓存应用场景_7.6二级缓存局限性

    7.4 mybatis整合ehcache EhCache 是一个纯Java的进程内缓存框架,是一种广泛使用的开源Java分布式缓存,具有快速.精干等特点,是Hibernate中默认的CacheProv ...