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 ...
随机推荐
- 用 Django 管理现有数据库
在多数项目中,总有一些几乎一成不变的 CRUD 操作,编写这些代码很无聊,但又是整个系统必不可少的功能之一.我们在上一个项目中也面临类似的问题,虽然已经实现了一个功能相对完整的管理后台,也尽量做到了代 ...
- Elicpse使用技巧-打开选中文件文件夹或者包的当前目录
很多时候,我们需要在eclipse那里打开选中文件(文件夹,包)的当前目录,在资源管理器那里显示这个目录,这个时候,我们又不想采用“选中文件/文件夹/包名--右击--Properties--Locat ...
- 【NLP】Attention Model(注意力模型)学习总结
最近一直在研究深度语义匹配算法,搭建了个模型,跑起来效果并不是很理想,在分析原因的过程中,发现注意力模型在解决这个问题上还是很有帮助的,所以花了两天研究了一下. 此文大部分参考深度学习中的注意力机制( ...
- Hadoop重新格式化HDFS的方法
1.查看hdfs-site.xml: <property> <name>dfs.name.dir</name> <value>/home/hadoop/ ...
- lambda函数
1.lambda函数是语法简短的匿名函数 2.lambda函数可以接受一个或多个参数 3.lambda函数只能有一个表达式 4.一般用于非重用的代码块 1)g = lambda x : x**2 g( ...
- Leetcode 143. Reorder List(Medium)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- H5 video标签的属性
35-video标签 video标签的属性 src: 用于告诉video标签需要播放的视频地址 autoplay: 用于告诉video标签是否需要自动播放视频 controls: 用于告诉video标 ...
- JUnit的配置及使用
一.安装插件JUnitGenertor V2.0 File->Setting->Plugins->在搜索框里输入JUintGenerator V2.0 二.导入JUnit相关jar包 ...
- MYSQL 三元 函数
mysql函数之流程控制-FreeOAhttp://www.freeoa.net/osuport/db/mysql-control-fun_2143.html mysql如何利用三元算法判断数字奇偶性 ...
- 解决jenkins运行磁盘满的问题
解决jenkins运行磁盘满的问题 - ling811的专栏 - CSDN博客 https://blog.csdn.net/ling811/article/details/74991899 1.自动丢 ...