cf- Educational Codeforces Round 40 -D
题意:给你n个点,m条边,一个起点s,一个终点t的无向图,问在某两个点之间加一条边,不改变s到t的最短路径的值的加法有多少种,所有点一定连接;
思路:首先,默认相邻两点的权值都为1,会改变值的情况有:
从s出发,算出s的单源最短路dist,如果dist[x]+1<dist[t];
从t出发,算出t的单源最短路Dist,如果Dist[x]+1<Dist[s];
介于两点之间,s—t之间的某两个点之间+1<dist[s],也就是:dist[x]+Dist[y]+1<(Dist[s] | | dist[t]);
所以从s跑一遍,再从t跑一遍最短路,然后遍历所有点,把能改变值的情况算出来;
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<set>
#include<cmath>
#define maxn 100005
const int inf=99999;
using namespace std;
struct Edge
{
int next;
int to;
int w;
}edge[maxn];
struct node
{
int num;
int dist;
node(int _num=0,int _dist=0):num(_num),dist(_dist){}
friend bool operator<(node a,node b)
{
return a.dist>b.dist;
}
};
int head[maxn];
int s[maxn];
int n,m,cnt;
int dis[maxn];
int dise[maxn];
int disb[maxn];
int book[1005][1005];
//int book[maxn];
bool vis[maxn];
int be,en;
void add(int u,int v,int w)
{
edge[cnt].next=head[u];
edge[cnt].to=v;
edge[cnt].w=w;
head[u]=cnt++;
}
void dij(int x)
{
priority_queue<node>que;
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[x]=0;
que.push(node(x,0));
while(!que.empty())
{
node p=que.top();
que.pop();
int now=p.num;
if(vis[now])
continue;
vis[now]=true;
for(int i=head[now];i!=-1;i=edge[i].next)
{
Edge e=edge[i];
if(dis[e.to]>dis[now]+e.w&&!vis[e.to])
{
dis[e.to]=dis[now]+e.w;
que.push(node(e.to,dis[e.to]));
}
}
}
}
int main()
{
int x,y,w;
cin>>n>>m>>be>>en;
cnt=0;
memset(book,0,sizeof(book));
memset(head,-1,sizeof(head));
for(int i=1;i<=m;i++)
{
cin>>x>>y;
add(x,y,1);
add(y,x,1);
}
int zz=0;
int ans=0;
for(int i=1;i<=n-1;i++)
zz+=i;
dij(en);
for(int i=1;i<=n;i++)
dise[i]=dis[i];
dij(be);
for(int i=1;i<=n;i++)
disb[i]=dis[i];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
continue;
if(disb[i]+dise[j]+1<dise[be])
{
if(book[i][j]==0&&book[j][i]==0)
{
ans++;
}
book[i][j]=book[j][i]=1;
}
else if(disb[j]+1<dise[be]-1)
{
if(book[j][en]==0&&book[en][j]==0)
ans++;
book[j][en]=book[en][j]=1;
}
else if(dise[j]+1<disb[en])
{
if(book[be][j]==0&&book[j][en]==0)
ans++;
book[j][be]=book[be][j]=1;
}
}
}
// cout<<ans<<endl;
zz-=m;
cout<<zz-ans<<endl;
return 0;
}
---恢复内容结束---
cf- Educational Codeforces Round 40 -D的更多相关文章
- Educational Codeforces Round 40千名记
人生第二场codeforces.然而遇上了Education场这种东西 Educational Codeforces Round 40 下午先在家里睡了波觉,起来离开场还有10分钟. 但是突然想起来还 ...
- Educational Codeforces Round 40 F. Runner's Problem
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
- Educational Codeforces Round 40 C. Matrix Walk( 思维)
Educational Codeforces Round 40 (Rated for Div. 2) C. Matrix Walk time limit per test 1 second memor ...
- Educational Codeforces Round 40 (Rated for Div. 2) Solution
从这里开始 小结 题目列表 Problem A Diagonal Walking Problem B String Typing Problem C Matrix Walk Problem D Fig ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- CF Educational Codeforces Round 3 E. Minimum spanning tree for each edge 最小生成树变种
题目链接:http://codeforces.com/problemset/problem/609/E 大致就是有一棵树,对于每一条边,询问包含这条边,最小的一个生成树的权值. 做法就是先求一次最小生 ...
- Educational Codeforces Round 40 I. Yet Another String Matching Problem
http://codeforces.com/contest/954/problem/I 给你两个串s,p,求上一个串的长度为|p|的所有子串和p的差距是多少,两个串的差距就是每次把一个字符变成另一个字 ...
- Educational Codeforces Round 40 (Rated for Div. 2) 954G G. Castle Defense
题 OvO http://codeforces.com/contest/954/problem/G 解 二分答案, 对于每个二分的答案值 ANS,判断这个答案是否可行. 记 s 数组为题目中描述的 a ...
- Educational Codeforces Round 40 G. Castle Defense (二分+滑动数组+greedy)
G. Castle Defense time limit per test 1.5 seconds memory limit per test 256 megabytes input standard ...
- CF# Educational Codeforces Round 3 F. Frogs and mosquitoes
F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input stan ...
随机推荐
- face recognition[翻译][深度人脸识别:综述]
这里翻译下<Deep face recognition: a survey v4>. 1 引言 由于它的非侵入性和自然特征,人脸识别已经成为身份识别中重要的生物认证技术,也已经应用到许多领 ...
- Vue-校验props传来的值
对父组件传来的值进行校验. Vue.component('child',{ props:{ content:{ type:String, required:false, default:'li zha ...
- Netty入门(一)之webSocket聊天室
一:简介 Netty 是一个提供 asynchronous event-driven (异步事件驱动)的网络应用框架,是一个用以快速开发高性能.高可靠性协议的服务器和客户端. 换句话说,Netty 是 ...
- OO博客作业2:第5-7周作业总结
(1)从多线程的协同和同步控制方面,分析和总结自己三次作业来的设计策略及其变化. 第5次作业:多线程电梯 基本照搬了课件上“生产者-消费者”模型的设计策略,将InputHandler设计为生产者线程, ...
- VirtualBox安装复制Centos6.6配置网络
由于要搭建mongodb的集群,先用虚拟机做下相关实验,以前都用VM Vare,但是现在这个电脑的配置不是太好,VM Vare比较耗资源,所以选择VirtualBox. 1.下载VirtualBox和 ...
- Full Regularization Path for Sparse Principal Component Analysis
目录 背景 Notation Sparse PCA Semidefinite Relaxation Low Rank Optimization Sorting and Thresholding 背景 ...
- Python—time模块介绍
time 模块 在平常的代码中,我们常常需要与时间打交道.在Python中,常用的与时间处理有关的模块就包括:time,datetime,下面来介绍time模块. 在开始之前,首先要说明几点: 一.在 ...
- codeforces#552 D. Vanya and Triangles(几何)
题意:给出n个不同的点,问能组成多少个不同的三角形 题解:对于每个点对,我们生成一个直线,用a*x+b=y表示,用map记录ab,这样就确定了一个直线,这样我们就能算出有多少点是共线的,这样复杂度就是 ...
- MySQL 通过多个示例学习索引
最近在准备面试,关于索引这一块,发现很多以前忽略的点,这里好好整理一下 首先为什么要建立索引 一本书,有章.节.段.行这种单位. 如果现在需要找一个内容:第9章>第2节>第3段>第4 ...
- CodeForces Round #529 Div.3
http://codeforces.com/contest/1095 A. Repeating Cipher #include <bits/stdc++.h> using namespac ...