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 ...
随机推荐
- RPC-基于原生java实现
一:什么是RPC 远程过程调用(Remote Procedure Call).就是调用其他业务方的方法的时候,就像是调用自己本地的方法一样. 二:java rpc实现简介 服务端(使用反射) (1)服 ...
- 以区间DP为前提的【洛谷p1063】能量项链
(跑去练习区间DP,然后从上午拖到下午qwq) 能量项链[题目链接] 然后这道题也是典型的区间DP.因为是项链,所以显然是一个环,然后我们可以仿照石子合并一样,把一个有n个节点的环延长成为有2*n个节 ...
- 汇编移位: SHL、SHR、SAL、SAR、ROL、ROR、RCL、RCR
SHL.SHR.SAL.SAR: 移位指令 ;SHL(Shift Left): 逻辑左移 ;SHR(Shift Right): 逻辑右移 ;SAL(Shift Arithmetic ...
- Linux安装了mysql 无法远程连接
问题: 本地安装完mysql,无法远程连接 1.检查mysql进程是否启动 ps -ef|grep -i mysql 2.查看端口是否监听 netstat -ntlp 3.查看iptables配置 v ...
- Log4Net 之将自定义属性记录到文件中 (三)
原文:Log4Net 之将自定义属性记录到文件中 (三) 即解决了将自定义属性记录到数据库之后.一个新的想法冒了出来,自定义属性同样也能记录到文件中吗?答案是肯定的,因为Log4Net既然已经考虑到了 ...
- 《死磕 Elasticsearch 方法论》:普通程序员高效精进的 10 大狠招!(完整版)
原文:<死磕 Elasticsearch 方法论>:普通程序员高效精进的 10 大狠招!(完整版) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链 ...
- Taro -- 获取用户手机号
1. 安装 Taro 脚手架工具 安装 Taro 开发工具 @tarojs/cli 使用 npm 或者 yarn 全局安装 $ npm install -g @tarojs/cli $ yarn gl ...
- 一、touch.js
一.touch.js 1.引用链接: <script src="https://cdn.bootcss.com/touchjs/0.2.14/touch.min.js"> ...
- javascript笔记收集
因为前端编程的兴起, 慢慢地对css/javascript越来越淡, 偶尔用一下,得查半天资料. 这里就收藏一下比较生僻, 但是做工具时会用到的. json获取属性名 Object.keys(), 只 ...
- 使用JavaBean对象存储表格数据
范例:表格内容接上篇 package cn.sxt.collection; import java.util.ArrayList;import java.util.Date;import java.u ...