CodeForces 25C(Floyed 最短路)
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 ai, bi, ci (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
2
0 5
5 0
1
1 2 3
3
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
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 最短路)的更多相关文章
- codeforces DIV2 D 最短路
http://codeforces.com/contest/716/problem/D 题目大意:给你一些边,有权值,权值为0的表示目前该边不存在,但是可以把0修改成另外一个权值.现在,我们重新建路, ...
- [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的过程中,顺便更新 ...
- Dynamic Shortest Path CodeForces - 843D (动态最短路)
大意: n结点有向有权图, m个操作, 增加若干边的权重或询问源点为1的单源最短路. 本题一个特殊点在于每次只增加边权, 并且边权增加值很小, 询问量也很小. 我们可以用johnson的思想, 转化为 ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
- 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 ...
- World Tour CodeForces - 667D (bfs最短路)
大意: 有向图, 求找4个不同的点ABCD, 使得d(A,B)+d(D,C)+d(C,A)最大
- 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 ...
- CodeForces 689B【最短路】
题意: 给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1. 思路: 直接建边,跑spfa就好了.虽然说似乎题意说边很多,其实只要建一下相邻的 ...
- Make It One CodeForces - 1043F (数论,最短路,好题)
大意: 给定序列$a$, 求最小子集, 使得gcd为1. 对于数$x$, 素因子多少次幂是无关紧要的, 这样就可以用一个二进制数来表示. $x$取$gcd$后的二进制状态最多$2^7$, 可以暴力枚举 ...
随机推荐
- fbset
fbset用于读取和设置framebuffer的参数. # fbset mode "800x480-112" # D: 64.998 MHz, H: 58.034 kHz, V: ...
- IoC最大的好处是什么?
IoC最大的好处是什么?因为把对象生成放在了XML里定义,所以当我们需要换一个实现子类将会变成很简单(一般这样的对象都是实现于某种接口的),只要修改XML就可以了,这样我们甚至可以实现对象的热插拨(有 ...
- R语言中两个数组(或向量)的外积怎样计算
所谓数组(或向量)a和b的外积,指的是a的每个元素和b的每个元素搭配在一起相乘得到的新元素.当然运算规则也可自己定义.外积运算符为 %o%(注意:百分号中间的字母是小写的字母o).比如: > a ...
- 基于bootstrap的select(可多选)
如图:
- css两列等高布局
布局方案 等高布局有几种不同的方法,但目前为止我认为浏览器兼容最好最简便的应该是padding补偿法.首先把列的padding-bottom设为一个足够大的值,再把列的margin-bottom设一个 ...
- 【java】 java 中stop方法终止线程的不良后果
stop()方法属于暴力终止线程的方法,有诸多隐患已经被废弃了. 下面演示的是stop方法会释放锁,造成数据不一致的问题. package com.xwolf.java.thread; /** * C ...
- mybatis由浅入深day01_4入门程序_4.6根据用户id(主键)查询用户信息
4 入门程序 4.1 需求 根据用户id(主键)查询用户信息 根据用户名称模糊查询用户信息 添加用户 删除 用户 更新用户 4.2 环境 java环境:jdk1.7.0_72 eclipse:indi ...
- 下载SpringJar包
方法一: 地址:http://repo.spring.io/release/org/springframework/spring/ 此方法简单. 方法二: 安装TortoiseSVN后,在电脑的任意空 ...
- com.alibaba.fastjson.JSONException: default constructor not found. class ……
1.json工具类 package com.hyzn.fw.util; import java.util.List; import java.util.Map; import com.alibaba. ...
- UE4射线的碰撞与绘制
http://blog.csdn.net/qq992817263/article/details/51800657 //起点 终点 FHitResult RayGetHitResult(FVector ...