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. 最简单的iOS网络请求

    做iOS开发,说到网络请求,大家可能都不约而同的提到AFN,可以说大家的网络请求都是用AFN封装而成,AFN的强大易用的确很好. 但是版本升级就会出现一些问题,所以就自己基于iOS原生封装了一个网络请 ...

  2. chrome播放m3u8視頻失败

    由于项目后台需要播放m3u8视频,但此视频格式在移动端和Safari支持比较友善但是PC浏览器中都不太尽如人意,所以想在Chrome中播放只能借助第三方插件来播放. 有一款Video.js插件极大的简 ...

  3. 软件测试_Loadrunner_APP测试_性能测试_脚本录制_基本操作流程

    这次主要是写一下使用Loadrunner对APP进行性能测试的基本流程,有关性能测试监控指标请查看链接:软件测试_性能测试_关注点. 先决条件:已安装Loadrunner.如未安装,请查看链接:软件测 ...

  4. UE4中Timeline的使用

    UE4中经常需要一些和时间相联系的功能,例如在一段时间内完成一个动作,播放一段动画,或者只是单纯的延迟函数的执行时间,即调整事件的执行顺序.在UE4的蓝图自带函数中有一个很好用的函数可以完美地解决这些 ...

  5. pycharm常用的一些快捷键

    1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + Enter 语句完成Ctrl + ...

  6. ERP条码解决方案,金蝶盘点机条码解决方案,应用PDA的信息化管理能给我们的生产管理带来怎么样的变化的探讨

    ERP条码解决方案,金蝶盘点机条码解决方案,应用PDA的信息化管理能给我们的生产管理带来怎么样的变化的探讨. 当前越来越多的大大小小的中国企业已经接受了ERP的思想,大多数的商店,企业,工厂都会上一套 ...

  7. 微软职位内部推荐-Senior NLP Scientist

    微软近期Open的职位: Job Title: Senior NLP Scientist Location: Suzhou, China Suzhou, one of the most vibrant ...

  8. 如何解决python连接数据库编码问题(python传数据到mysql乱码)'ascii' codec can't encode _mysql_exceptions.OperationalError: (1366, "Incorrect string value:?

    首先描述下问题:  在使用python计算出结果后将结果插入到mysql过程中,报如下错误.原因很好定位就是编码的问题.那么到底是编码哪里出了问题了呢? 报错如上: 排查顺序: 第一:python的编 ...

  9. 冲刺Two之站立会议1

    今天我们开始了第二个冲刺期的工作,大家重新讨论了下个阶段的工作内容,由于上次演示我们主要只是实现了摄像头开启.通信和语音通话的功能,而且各部分还有待完善.所以我们决定了之后的主要工作的内容:之前服务器 ...

  10. 第三次作业--导入excel表格(完整版)

    031302322 031302316 将教师排课表导入系统 使用powerdesigner设计数据库表格 设计概念模型 打开new -> Conceptual Data Model创建概念模型 ...