CF-25C - Roads in Berland(水题)
来源:互联网 发布:js字符串转base64编码 编辑:程序博客网 时间:2024/04/29 13:17
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 buildk 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 follown 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 citiesi 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. Followingk lines contain the description of the planned roads. Each road is described by three space-separated integersai,bi,ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai andbi — 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 toi. 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
20 55 011 2 3
3
30 4 54 0 95 9 022 3 81 2 1
17 12
思路:每次更新新加的边所影响的所有城市的最小距离。
#include<iostream>#include<cstring>using namespace std;const int mm=310;int map[mm][mm];int main(){ int m,n; while(cin>>m) { memset(map,0,sizeof(map)); for(int i=0;i<m;i++) for(int j=0;j<m;j++) cin>>map[i][j]; cin>>n; int a,b,c; long long sum; for(int i=0;i<n;i++) { cin>>a>>b>>c; sum=0; a--;b--; for(int j=0;j<m;j++) for(int k=0;k<m;k++) { map[j][k]=min(min(map[j][k],map[j][a]+map[b][k]+c),map[j][b]+map[a][k]+c); sum+=map[j][k]; } cout<<sum/2<<" "; } cout<<"\n"; }}
- CF-25C - Roads in Berland(水题)
- CF 25C Roads in Berland
- codeforces 25C. Roads in Berland
- CodeForces 25C Roads in Berland
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Codeforces Beta Round #25 C题 Roads in Berland
- CodeForces 25C Roads in Berland(图论-最短路径)
- Codeforces Beta Round #25 (Div. 2 Only), problem: (C) Roads in Berland 图论
- Codeforces 25D Roads not only in Berland(并查集)
- CodeForces 25D Roads not only in Berland (并查集)
- Codeforces 25 D.Roads not only in Berland(并查集)
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- CodeForces 25D Roads not only in Berland
- CF#1C Ancient Berland Circus(正多边形最小面积)
- Lakes in Berland(CF #375 Div. 2)
- CF 1C Ancient Berland Circus
- Linux下的汇编语言之系统调用
- Creo二次开发--函数(七)--尺寸遍历操作函数
- android设备添加F1-F12按键功能
- Goldfish_events浅析
- February 2013 Critical Patch Update for Java SE Released
- CF-25C - Roads in Berland(水题)
- #IFNDEF、#DEF、#ENDIF 的使用
- JavaDoc那些事儿
- DOS系统功能调用表(INT 21H)
- 如何更改CMD命令的默认路径 (方便Java编程人员)
- CF-25D - Roads not only in Berland(并查集或者搜索)
- Django -- Request and HttpRequest
- 笔记2
- 2013年01月