http://acm.hdu.edu.cn/showproblem.php?pid=2433

题意:

求删除任意一条边后,任意两点对的最短路之和

以每个点为根节点求一个最短路树,

只需要记录哪些边在最短路树上,记录整棵树的dis和

如果删除的边不在最短路树上,累加记录的dis和

否则,重新bfs求dis和

因为最短路树上有n-1条边,n棵树,所以只有(n-1)*n条边需要重新bfs

时间复杂度为n*n*m

求桥是 对面的low>自己的dfn,我求了一年假的桥

my god ! ლ(ٱ٥ٱლ)

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; #define N 101
#define M 3001 int n,m; int tot;
int front[N],nxt[M<<],to[M<<]; bool use[N][M],ok[M];
int sum[N],dis[N];
queue<int>q; int dfn[N],low[N];
bool bridge[M]; void read(int &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void add(int u,int v)
{
to[++tot]=v; nxt[tot]=front[u]; front[u]=tot;
to[++tot]=u; nxt[tot]=front[v]; front[v]=tot;
} void tarjan(int u,int pre)
{
dfn[u]=low[u]=++tot;
for(int i=front[u];i;i=nxt[i])
{
if(i==(pre^)) continue;
if(!dfn[to[i]])
{
tarjan(to[i],i);
low[u]=min(low[u],low[to[i]]);
if(low[to[i]]>dfn[u]) bridge[i>>]=true;
}
else low[u]=min(low[u],dfn[to[i]]);
}
} void bfs(int s)
{
memset(dis,,sizeof(dis));
sum[s]=;
q.push(s);
int now;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=front[now];i;i=nxt[i])
if(to[i]!=s && !dis[to[i]])
{
use[s][i>>]=true;
dis[to[i]]=dis[now]+;
sum[s]+=dis[to[i]];
q.push(to[i]);
}
}
} int bfs2(int s)
{
int ans=;
memset(dis,,sizeof(dis));
q.push(s);
int now;
while(!q.empty())
{
now=q.front();
q.pop();
for(int i=front[now];i;i=nxt[i])
if(ok[i>>] && to[i]!=s && !dis[to[i]])
{
dis[to[i]]=dis[now]+;
ans+=dis[to[i]];
q.push(to[i]);
}
}
return ans;
} void solve()
{ int ans;
memset(ok,true,sizeof(ok));
for(int i=;i<=m;++i)
{
if(bridge[i])
{
puts("INF");
continue;
}
ans=;
for(int j=;j<=n;++j)
if(!use[j][i]) ans+=sum[j];
else
{
ok[i]=false;
ans+=bfs2(j);
ok[i]=true;
}
printf("%d\n",ans);
}
} void clear()
{
tot=;
memset(front,,sizeof(front));
memset(dfn,,sizeof(dfn));
memset(bridge,false,sizeof(bridge));
memset(use,false,sizeof(use));
} int main()
{
int u,v;
while(scanf("%d",&n)!=EOF)
{
clear();
read(m);
for(int i=;i<=m;++i)
{
read(u); read(v);
add(u,v);
}
for(int i=;i<=n;++i) bfs(i);
tot=;
tarjan(,);
bool tag=false;
for(int i=;i<=n;++i)
if(!dfn[i])
{
tag=true;
break;
}
if(!tag) solve();
else
for(int i=;i<=m;++i) puts("INF");
}
}

Travel

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3388    Accepted Submission(s): 1160

Problem Description
      One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always take the shortest way. There are M (M <= 3000) two-way roads connecting the towns, and the length of the road is 1.
      Let SUM be the total distance of the shortest paths between all pairs of the towns. Please write a program to calculate the new SUM after one of the M roads is destroyed.

 
Input
      The input contains several test cases.
      The first line contains two positive integers N, M. The following M lines each contains two integers u, v, meaning there is a two-way road between town u and v. The roads are numbered from 1 to M according to the order of the input.
      The input will be terminated by EOF.

 
Output
      Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
 
Sample Input
5 4
5 1
1 3
3 2
5 4
2 2
1 2
1 2
 
Sample Output
INF
INF
INF
INF
2
2

hdu 2433 Travel的更多相关文章

  1. HDU 2433 Travel (最短路,BFS,变形)

    题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...

  2. hdu 2433 Travel(还不会)

    Problem Description       One day, Tom traveled to a country named BGM. BGM is a small country, but ...

  3. hdu 2433 Travel (最短路树)

     One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...

  4. hdu 5380 Travel with candy(双端队列)

    pid=5380">题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别能够以多少价格退货,以及能够推货的量. ...

  5. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  6. HDU 2433 (最短路+BFS+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=2433 这个问题因为路径都是1,所以可以用bfs遍历 可以看这几篇文章讲解: http://blog.csdn.n ...

  7. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  8. hdu 5441 Travel(并查集)

    Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...

  9. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

随机推荐

  1. numpy 初识(一)

    基本操作: 读取文件(与pandas读取csv相似): import numpy numpy.genfromtxt("word.txt", delimiter=',', dtype ...

  2. Qt QpushButton 实现长按下功能

    做项目需要一个按钮具备长时间按下的功能,才发现Qt原始的按钮是没有这个功能,不过Qt的原生按钮是存在按下和释放信号的,有了这两个信号,再来实现按钮长时间被按下,这就简单了,看下动画演示. 录成GIF效 ...

  3. openstack删除僵尸卷

    问题描述: 最近在清理openstack环境,在删除cinder云硬盘时,一直发现有两个卷在删除中. 解决方法如下: 首先我们去cinder的数据库中找到这个卷,命令为: MariaDB [(none ...

  4. StackGAN 阅读笔记

    StackGAN 阅读笔记 StackGAN论文下载链接(arxiv) 创新点 提出多尺度的GAN Stage-I GAN Stage-II GAN Stage-I GAN 主要是根据文本描述抓取目标 ...

  5. ace how to guide

    Configuring the editor there are several ways to pass configuration to Ace 有几种方法可以将配置传递给ace // pass ...

  6. mysql学习(2)-Navicat Premium 12 链接MySQL8.0.11数据库报2059错误

    Navicat Premium 12 链接MySQL8.0.11数据库报2059错误 1,问题现象 安装完MySQL8.0.11和Navicat Premium12后,我们会用Navicat去测试连接 ...

  7. 《linux内核分析》作业一:分析汇编代码

    通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的(王海宁) 姓名:王海宁                             学号:20135103 课程:<Linux内核分析& ...

  8. 《Linux内核分析》 第一节 计算机是如何工作的

    第一节 计算机是如何工作的 张嘉琪 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-100002900 ...

  9. 『编程题全队』Alpha 阶段冲刺博客Day8

    1.每日站立式会议 1.会议照片 2.昨天已完成的工作统计 孙志威: 1.修复了看板任务框拖拽时候位置不够精确的问题 2.向个人界面下添加了工具栏 3.个人界面下添加了任务框测试 孙慧君: 1.个人任 ...

  10. Docker(二十一)-Docker Swarm集群部署

    介绍 Swarm 在 Docker 1.12 版本之前属于一个独立的项目,在 Docker 1.12 版本发布之后,该项目合并到了 Docker 中,成为 Docker 的一个子命令.目前,Swarm ...