C. Roads in Berland
题目链接:
http://codeforces.com/problemset/problem/25/C
题意:
给一个最初的所有点与点之间的最短距离的矩阵。然后向图里加边,原有的边不变,问加边后的各个顶点的距离是多少。
思路:
这个一看就知道是folyd的变种,关键是状态转移怎么处理,具体看代码。
代码:
#include<bits/stdc++.h>
#define LL long long using namespace std;
const int maxn=;
int dist[maxn][maxn];
LL ans;
void modify(int u,int v,int w)
{
if(dist[u][v]>w)
{
ans-=(dist[v][u]-w);
dist[u][v]=w;
}
return;
}
int main()
{
int n;
cin>>n;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>dist[i][j];
if(j>i)
{
ans+=dist[i][j];
}
} }
//cout<<ans<<endl;
int k;
cin>>k;
for(int i=;i<=k;i++)
{
int u,v,w;
cin>>u>>v>>w;
modify(u,v,w);
if(dist[u][v]!=dist[v][u])
{
dist[v][u]=dist[u][v];
}
//cout<<ans<<endl;
for(int p=;p<=n;p++)
{
for(int q=;q<=n;q++)
{
modify(p,q,dist[p][u]+dist[v][q]+w);
if(dist[p][q]!=dist[q][p])
{
dist[q][p]=dist[p][u]+dist[v][q]+w;
}
}
}
cout<<ans<<endl;
} return ;
}
C. Roads in Berland的更多相关文章
- Codeforces Beta Round #25 (Div. 2 Only) C. Roads in Berland
C. Roads in Berland time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Roads in Berland(图论)
Description There are n cities numbered from 1 to n in Berland. Some of them are connected by two-wa ...
- Day4 - M - Roads in Berland CodeForces - 25C
There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Eac ...
- 【Codeforces 25C】Roads in Berland
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 用floyd思想. 求出来这条新加的边影响到的点对即可. 然后尝试更新点对之间的最短路就好. 更新之后把差值从答案里面减掉. [代码] #in ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- CF 191C Fools and Roads lca 或者 树链剖分
They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, popu ...
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...
- codeforces 228E The Road to Berland is Paved With Good Intentions(2-SAT)
Berland has n cities, some of them are connected by bidirectional roads. For each road we know wheth ...
- CF191C Fools and Roads - 树剖解法
Codeforces Round #121 (Div. 1) C. Fools and Roads time limit per test :2 seconds memory limit per te ...
随机推荐
- Mysql8- Public Key Retrieval is not allowed
在使用 MySQL 8.0 时重启应用后提示 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public ...
- Waiting for table flush 阻塞查询的问题
1.此状态表示大量thread正在等待慢查询语句执行完成. 原因: The thread got a notification that the underlying structure for a ...
- 放弃等待,故障到来:少一个 await 引发的 tcp 连接泄漏故障
更新:后来升级至 .NET Core 2.2 Preview 3 ,并将 System.Net.Http 升级至 4.3.4 之后没出现这个问题,问题与 https://github.com/dotn ...
- sqlserver 高版本迁移到低版本
奇葩事不少, 这不, 得把 sqlserver 2014 迁移到 2012 开始以为用备份再还原的方法就可以, 谁知道最终兼容性的问题无法解决(低版本不兼容高版本备份的文件, 即便在高版本中选择了兼 ...
- Java JNA (四)—— void**、void*、char**、char*、int*等类型映射关系及简单示例
ByReference类有很多子类,这些类都非常有用. ByteByReference.DoubleByReference.FloatByReference. IntByReference.LongB ...
- Rsync+sersync部署
内核版本:2.6.32-431.el6.x86_64 系统采用最小化安装,系统经过了基本优化,selinux 为关闭状态,iptables 为无限制模式 源码包存放位置:/root Rsync 客户端 ...
- spring boot 加载指定xml
方法一:使用@ImportResource 方法二:在test中 @ContextConfiguration(locations = "classpath:spring-profile.xm ...
- CentOS 7系统安装nginx+php
安装介绍1.系统环境CentOS7 2.nginx版本1.12 3.PHP版本7.2 下载地址 4.MySQL版本5.7 安装nginx添加centos7的 nginx yum源 然后执行安装 sud ...
- 初学Java 数组统计字母
public class CountLetterInArray { public static void main(String[] args) { char[] chars = createArra ...
- bzoj4811 [Ynoi2017]由乃的OJ 树链剖分+贪心+二进制
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4811 题解 我现在为什么都写一题,调一天啊,马上真的退役不花一分钱了. 考虑这道题的弱化版 N ...