CodeForces 25C Roads in Berland【floyd过】
2016-02-17 15:21
417 查看
Roads in Berland
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 that di, 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
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
题目大意:每一次改变一个边的长度,输出各个点之间最短距离和、
一看各个点之间的最短路径和,floyd明显符合条件、
floyd递推最短路、但是300^4明显会超时,所以我们这里要优化代码,只判断和u,v(改变边长的两个点)这两个特殊的点就行了、
如果从j到k,能够通过u这个点把路径变短,那么就出现了一次优化路径。在经过u,v这两个点的判断的时候,不免就会优化到u,v这条边,所以代码这样写就ok了:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll long long int
ll map[305][305];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%I64d",&map[i][j]);
}
}
int k;
scanf("%d",&k);
while(k--)
{
int u,v;
ll val;
scanf("%d%d%I64d",&u,&v,&val);
if(val<map[u][v])
{
map[u][v]=val;
map[v][u]=val;
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
map[k][j]=map[j][k]=min(map[j][k],map[j][u]+map[u][k]);
}
}
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
map[k][j]=map[j][k]=min(map[j][k],map[j][v]+map[v][k]);
}
}
}
ll sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
sum+=map[i][j];
}
}
printf("%I64d\n",sum/2);
}
}
}
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 that di, 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
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
题目大意:每一次改变一个边的长度,输出各个点之间最短距离和、
一看各个点之间的最短路径和,floyd明显符合条件、
floyd递推最短路、但是300^4明显会超时,所以我们这里要优化代码,只判断和u,v(改变边长的两个点)这两个特殊的点就行了、
如果从j到k,能够通过u这个点把路径变短,那么就出现了一次优化路径。在经过u,v这两个点的判断的时候,不免就会优化到u,v这条边,所以代码这样写就ok了:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll long long int
ll map[305][305];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
scanf("%I64d",&map[i][j]);
}
}
int k;
scanf("%d",&k);
while(k--)
{
int u,v;
ll val;
scanf("%d%d%I64d",&u,&v,&val);
if(val<map[u][v])
{
map[u][v]=val;
map[v][u]=val;
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
map[k][j]=map[j][k]=min(map[j][k],map[j][u]+map[u][k]);
}
}
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
map[k][j]=map[j][k]=min(map[j][k],map[j][v]+map[v][k]);
}
}
}
ll sum=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
sum+=map[i][j];
}
}
printf("%I64d\n",sum/2);
}
}
}
相关文章推荐
- 使用stl中的 advance和 distance 方法来进行iterator的加减 http://blog.csdn.net/tangaowen/article/details/7515558
- Jenkins服务器安装与配置
- 基础练习 FJ的字符串(递归)
- 搜索关键字变红,指定字段变色 一行代码简单集成 搜索关键字变红,Android字体变红,指定字段变红
- 有源晶振和无源晶振的输出波形
- Linux系统扫描技术及安全防范
- trunc的使用
- IE版本判断
- 英语自勉
- Bootstrap入门书籍之(四)菜单、按钮及导航
- golang 阻塞的坑
- 编译器错误消息: CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)
- USACO1.1.1you ride is here
- jdk配置、jvm配置、远程debug配置样例
- Python和Powershell的相互调用
- 成功程序员的8个习惯
- 修改mysql数据文件保存路径
- ViewConfiguration.getScaledTouchSlop () 用法
- C语言 预处理一(文件包含--#include)
- 美食 - 汤