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 ...
随机推荐
- 《maven in action》部分知识点总结
maven in action 的部分知识点总结 今天又将<maven in action>这本书看了一遍,总结了一下,大概需要的知识点 (一)解耦 使用maven,在没有任何实际的J ...
- FineUIPro v4.0.0 发布了,全新 CSS3 动画惊艳登场!
FineUI(专业版)v4.0.0 即将于 2017-10-23 发布! 这个版本将引入了激动人心的 CSS3 动画,只需要开启全局属性 EnableAnimation 即可,先睹为快: 1. 菜单动 ...
- 史上最全的Spring Boot配置文件详解
Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门和基础,经常会用到,所以写下做个总结以便日 ...
- Web测试和App测试有什么区别
WEB测试和App测试从流程上来说,没有区别.都需要经历测试计划方案,用例设计,测试执行,缺陷管理,测试报告等相关活动.从技术上来说,WEB测试和APP测试其测试类型也基本相似,都需要进行功能测试.性 ...
- Python-每日习题-0009-time
题目:暂停一秒输出 程序分析:使用 time 模块的 sleep() 函数. import time for i in range(4): print(str(int(time.time()))[-2 ...
- Python-类的封装
1:封装数据 将数据隐藏起来这不是目的.隐藏起来然后对外提供操作该数据的接口,然后我们可以在接口附加上对该数据操作的限制,以此完成对数据属性操作的严格控制. class Teacher: def __ ...
- Python之参数类型、变量
一.参数类型 (一)形参与实参 要使用局部变量时,只能通过return的方式返回 def my(name): #函数体 return name my('lrx') #name是形参,lrx是实参 不写 ...
- PAT 7-12 拯救007
在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接踩着池子里一系列鳄鱼的大脑袋跳上岸去!(据 ...
- mysql cpu 100% 满 优化方案
解决MySQL CPU占用100%的经验总结 - karl_han的专栏 - CSDN博客 https://blog.csdn.net/karl_han/article/details/5630782 ...
- withRouter使用
import React from 'react'; import {Switch,NavLink,Route,Redirect,withRouter} from 'react-router-dom' ...