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;
}

最新文章

  1. Android自定义对话框
  2. property内存管理策略
  3. Flex contextMenu
  4. eclipse安卓模拟器窗口大小调整
  5. GUI_DOWNLOAD参数说明
  6. 自定义表单样式之checkbox和radio
  7. Effective C++ 学习总结
  8. 线段树(build,insert,dfs操作)
  9. 常见JS挂马方法及如何防止网站被黑客挂马?
  10. ArcGIS API for JavaScript 4.2学习笔记[19] 搜索小部件——使用更多数据源
  11. 关于fprint()和fwrite()
  12. RHEL简单管理SELINUX
  13. 关于PCB走线能不能走锐角的讨论
  14. swust oj 987
  15. 灰熊:在这6个信息流和DSP平台投放后,我总结了这些血泪经验!
  16. 不一样的go语言-error
  17. Qt+数据库发布后无法打开数据库
  18. Find the peace with yourself
  19. hdu 5074 相邻数和最大dp
  20. mybatis 注解和xml 优缺点

热门文章

  1. android activity lifecycle
  2. 10篇写给Git初学者的最佳教程(转)
  3. 内网DNS投毒技术劫持会话
  4. (转)Spring开启Annotation&lt;context:annotation-config&gt; 和 &lt;context:component-scan&gt;诠释及区别
  5. IIS 使用多个https和通配证书解决方案
  6. css文字超出自动显示省略号
  7. Entity FrameWork实现增、删、改、分页的数据层代码实现
  8. quick-cocos2d-x游戏开发【10】——触摸捕获事件 cc.NODE_TOUCH_CAPTURE_EVENT
  9. POJ 3211 Washing Cloths(01背包变形)
  10. HTML5开发之 -- 模态突出窗(bootstrap)