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. JavaScript快速入门-ECMAScript基础语法

    一.JavaScript引入方式 1.行内式 <script> alert(123); </script> 2.外链式 <script src='custom.js'&g ...

  2. 异步编程之asyncio简单介绍

    引言: python由于GIL(全局锁)的存在,不能发挥多核的优势,其性能一直饱受诟病.然而在IO密集型的网络编程里,异步处理比同步处理能提升成百上千倍的效率,弥补了python性能方面的短板. as ...

  3. 汉码盘点机PDA无缝对接思迅思迅盘点机思迅条码数据采集器批号商品盘点的方法

    1.1.    盘点批号 如果某些商品进行了批号管理,我们不仅仅要清点什么商品总数有多少个,我们还要区分该商品的某个批号有多少个数量,因此以前批号盘点工作量是非常大的. 我们的盘点机PDA支持批号盘点 ...

  4. Istio全景监控与拓扑

    根据Istio官方报告,Observe(可观察性)为其重要特性.Istio提供非侵入式的自动监控,记录应用内所有的服务. 我们知道在Istio的架构中,Mixer是管理和收集遥测信息的组件.每一次当请 ...

  5. 开源一个最近写的spring与mongodb结合的demo(spring-mongodb-demo)

    由于工作需要,给同事们分享了一下mongodb的使用,其中主要就是做了一个spring-data+mongodb的小例子,本着分享的精神,就上传到了github.com上,有需要的同学请移步githu ...

  6. 2-Thirteenth Scrum Meeting-10151213

    任务安排 成员 今日完成 明日任务 闫昊 获取视频播放进度  用本地数据库记录课程结构和学习进度 唐彬  阅读IOS代码+阅读上届网络核心代码  请假(编译……) 史烨轩 下载service开发    ...

  7. C/C+ 感触

    1.       C/C++语言开发的首选利器- C++Test       以前在windows平台下的开发,使用的框架主要是MFC,以及console工程(基于win32SDK),属于纯C/C++ ...

  8. Java标识符和关键字(static,final,abstract,interface)

    本文的主要内容如下   1.标识符合关键字 2.Java中的关键字 3.static关键 字 4.static方法 5.静态代码块 6.static修饰符综述 7.final关键字 8.final修饰 ...

  9. 通过第三方软件打开sqlite

    1.SQLite Expert 使用之前,可以先下载SQLite Expert,方便查看.db3数据库 下载链接是http://www.sqliteexpert.com/    可以谷歌找到licen ...

  10. Python的文件读写

    目录 读文件 操作文件 读取内容 面试题的例子 写文件 操作模式 指针操作 字符编码 读文件 操作文件 打开一个文件用open()方法(open()返回一个文件对象,它是可迭代的): 文件使用完毕后必 ...