codeforces 25C. Roads in Berland

旧城等待, 2022-08-03 08:23 106阅读 0赞

有n个城市,每个城市都能到达别的城市,n*n的矩阵表明i到j城市的最短距离,现在要建造一些新的道路,在两个城市之间。问每次建造这些道路之后每两个城市之间的距离之和为多少。

如果新建的道路比当前的最短道路要长,那么总和不会更新,如果要短的话,那么要把当前道路最短距离更新。

同时枚举其他各个城市之间的距离有没有变小。假设t1到t2之间距离更新了,对于任意两个城市i,j,要比较一下从i到j的最短距离是不是比

从i到t1,再从t1到t2,再从t2到j的距离和大,,大的话就更新。注意没说最短距离的范围,要用long long

  1. #include<map>
  2. #include<vector>
  3. #include<cstdio>
  4. #include<iostream>
  5. #include<cstring>
  6. #include<string>
  7. #include<algorithm>
  8. #include<cmath>
  9. #include<stack>
  10. #include<queue>
  11. #include<set>
  12. #define inf 0x3f3f3f3f
  13. #define mem(a,x) memset(a,x,sizeof(a))
  14. using namespace std;
  15. typedef long long ll;
  16. typedef pair<int,int> pii;
  17. inline int in()
  18. {
  19. int res=0;char c;
  20. while((c=getchar())<'0' || c>'9');
  21. while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
  22. return res;
  23. }
  24. int maze[333][333];
  25. int main()
  26. {
  27. int n=in();
  28. ll ans=0;
  29. for(int i=1;i<=n;i++)
  30. {
  31. for(int j=1;j<=n;j++)
  32. {
  33. maze[i][j]=in();
  34. ans+=maze[i][j];
  35. }
  36. }
  37. ans>>=1;
  38. int k=in();
  39. for(int ii=0;ii<k;ii++)
  40. {
  41. int t1=in(),t2=in(),t3=in();
  42. if(t3>=maze[t1][t2])
  43. {
  44. printf("%I64d\n",ans);
  45. continue;
  46. }
  47. ll cnt=maze[t1][t2]-t3;
  48. maze[t1][t2]=t3;
  49. maze[t2][t1]=t3;
  50. for(int i=1;i<=n;i++)
  51. {
  52. for(int j=1;j<=n;j++)
  53. {
  54. if(maze[i][j]>maze[i][t1]+maze[t1][t2]+maze[t2][j])
  55. {
  56. cnt+=maze[i][j]-maze[i][t1]-maze[t1][t2]-maze[t2][j];
  57. maze[i][j]=maze[i][t1]+maze[t1][t2]+maze[t2][j];
  58. maze[j][i]=maze[i][j];
  59. }
  60. }
  61. }
  62. ans-=cnt;
  63. printf("%I64d ",ans);
  64. }
  65. return 0;
  66. }

发表评论

表情:
评论列表 (有 0 条评论,106人围观)

还没有评论,来说两句吧...

相关阅读

    相关 codeforces 25C. Roads in Berland

    有n个城市,每个城市都能到达别的城市,n\n的矩阵表明i到j城市的最短距离,现在要建造一些新的道路,在两个城市之间。问每次建造这些道路之后每两个城市之间的距离之和为多少。 如

    相关 B. Berland Crossword (构造)

    [题目][Link 1] 对于下面这个图可以知道当U涂中间三个时是不会对其它的三种产生限制的,而如果涂了4个则必然会占到L R其中一个相邻的格子,涂了5个必然会占掉相邻的