首页 > 其他分享 >CF-25C - Roads in Berland(水题)

CF-25C - Roads in Berland(水题)

时间:2023-02-24 10:33:26浏览次数:51  
标签:map int 25C CF roads Roads integer cities shortest



C - Roads in Berland

Crawling in process...

Crawling failed

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u


​Submit​​​ ​​​Status​​​ ​​​Practice​​​ ​​​CodeForces 25C​


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



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



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



思路:每次更新新加的边所影响的所有城市的最小距离。

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





标签:map,int,25C,CF,roads,Roads,integer,cities,shortest
From: https://blog.51cto.com/u_15953788/6082908

相关文章

  • CF-25D - Roads not only in Berland(并查集或者搜索)
    D-RoadsnotonlyinBerlandCrawlinginprocess...CrawlingfailedTimeLimit:2000MSMemoryLimit:262144KB64bitIOFormat:%I64d&%I64u​​Submit​​......
  • CF737E Tanya is 5!
    全在口胡,没写代码首先不考虑复制。显然是一个二分图匹配问题。如果能分成\(k\)次若干组匹配,那么答案一定不超过\(k\)。建出二分图,答案有下界:最大的度数。想象一下,每......
  • CF611H New Year and Forgotten Tree
    首先注意到:任何合法方案一定能调整成:每种位数选一个关键点,每条边都至少有一个关键点。本质上是希望找一个边和点的匹配。一种思路是确定关键点之间形成的树后(暴力枚举),让......
  • CF245H Queries for Number of Palindromes
    对字符串s,多次询问,给你两个数L和R,问在字符串区间l到r的字串中,包含多少回文串。 #include<iostream>#include<queue>#include<cstring>#defineIOSstd::ios::syn......
  • 【五期邹昱夫】CCF-A(ICCV'21)On the Difficulty of Membership Inference Attacks
    "Rezaei,Shahbaz,andXinLiu."Onthedifficultyofmembershipinferenceattacks."ProceedingsoftheIEEE/CVFConferenceonComputerVisionandPatternRec......
  • CF818G - Four Melody
    题意:对于一个序列,令一个\(melody\)为一个子序列满足子序列的相邻两项相差\(1\)或者模\(7\)同余。现在提取四个不重合的\(melody\),求最长总长度。我们先考虑暴力的......
  • CF818F - Level Generation
    题意:假设当前有\(n\)个点,求最多的边数,使得桥的数量\(\ge\lceil\dfrac{m}{2}\rceil\)。我们考虑构造,首先,整张图一共只有一个双连通分量。因为我们如果有两个双连通分量,......
  • CF825G - Tree Queries
    现在有\(n\)次操作,每次将一个点设为黑色,或者查询:从当前点到任意黑点路径上最小值的最小值。保证第一次操作是设置黑点。强制在线。我们考虑这样一个过程,我们把第一次操......
  • CF825F - String Compression
    题意:压缩字符串,把字符串分成若干个子串,每个子串可以被压缩成“循环次数\(+\)循环节”的形式,求最小长度。dp求lcp先\(O(n^2)\)dp求出所有后缀对的\(lcp_{x,y}\),(也......
  • CF845G - Shortest Path Problem?
    题意:求带边权无向图上\(1\)到\(n\)的异或最短路,可以重复经过某条边。首先,我们考虑从\(x\)到\(y\)的路径\(A\),它的权值是\(a\)。我们从路径中途的某个地方离开路......