CF954D Fight Against Traffic
题意描述:
给你一张无向图,一共有n个点(2 <= n <= 1000),由m条边连接起来(1 <= m <= 10000),现在要在任意一对没有连边的点之间连上一条边,并且保证s到t之间的最短路径长度不变(最短路径长度表示s到t最少经过的边的数量)(1 <= s,t <= n , s≠t),请你求出一共有多少条这样的边。
被水题成功报复。。。
code:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
inline int read(){
int sum=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
while(ch>='0'&&ch<='9'){sum=(sum<<1)+(sum<<3)+ch-'0'; ch=getchar();}
return sum*f;
}
const int wx=2017;
int head[wx],dis1[wx],dis2[wx];
int vis[wx],fl[wx][wx];
int num,n,m,s,t,ans;
struct e{
int nxt,to,dis;
}edge[wx*2];
void add(int from,int to,int dis){
edge[++num].nxt=head[from];
edge[num].to=to;
edge[num].dis=dis;
head[from]=num;
}
struct node{
int u,d;
friend bool operator < (const node & a,const node & b){
return a.d>b.d;
}
};
priority_queue<node > q;
void Dij(int S,int dis[]){
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f,vis[i]=0;
dis[S]=0; q.push((node){S,0});
while(q.size()){
int u=q.top().u; q.pop();
if(vis[u])continue; vis[u]=1;
for(int i=head[u];i;i=edge[i].nxt){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].dis){
dis[v]=dis[u]+edge[i].dis;
q.push((node){v,dis[v]});
}
}
}
}
int main(){
n=read(); m=read();
s=read(); t=read();
for(int i=1;i<=m;i++){
int x,y;
x=read(); y=read();
add(x,y,1); add(y,x,1);
fl[x][y]=1; fl[y][x]=1;
}
Dij(s,dis1); Dij(t,dis2);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(fl[i][j])continue;
int tmp1=dis1[i]+dis2[j]+1;
int tmp2=dis1[j]+dis2[i]+1;
if(min(tmp1,tmp2)>=dis1[t])ans++;
}
}
printf("%d
",ans);
return 0;
}