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. kubernetes 网络故障遇见的坑

    1.记录一下自己搭建kubernetes 集群遇见的坑. 过程是我学技术以来最大的bug,处处都是坑,稍微写成一点, 就完全起不来, 起不来之后, 还找不到故障点, 郁闷之极. 后续会慢慢分享给大家. ...

  2. RabbitMq基础教程之基本概念

    RabbitMq基础教程之基本概念 RabbitMQ是一个消息队列,和Kafka以及阿里的ActiveMQ从属性来讲,干的都是一回事.消息队列的主要目的实现消息的生产者和消费者之间的解耦,支持多应用之 ...

  3. asp.net 网页拉伸 到300%不变形方法一

    网页拉伸到300%控件和表格不会出现太大变形 方法: 1.对主页面采用百分比宽度(Width="100%") 2.对于表格使用百分比宽度,包括表格宽度和表格中顶端td宽度 3.对t ...

  4. 《实时控制软件设计》之Github提交作业步骤

    在掌握GIT/GITHUB基本操作后,接下来把第一次的编程作业提交到 https://github.com/RTCSD15/HOMEWORK1 ,把第二次的编程作业提交到https://github. ...

  5. Game over 作业

    终于有一篇不拼代码拼码字的作业了,哈哈哈..... 从寒假到这次结束,经历的博客及编码作业的过程 前面七次作业做个分类: 通往博客园和C++的第一步. 知识点:让我们对C++做一个预习,在学C++前有 ...

  6. bash基本功能 -命令的别名和快捷键

    命令的别名 == 人的小名 如何查看和设定别名 alias 查看系统中的所有别名 ls --color=auto alias ll = 'ls - l --color=auto' touch abc ...

  7. Docker(十八)-Docker配置DNS

    Linux系统配置DNS的时候有一个问题,就是你在/ect/resolv.conf文件中添加上nameserver XXX.XXX.XXX.XXX的时候,当时是生效的,但是机器重启之后就失效了,所以我 ...

  8. [转帖]SSL/TLS/WTLS原理

    SSL/TLS/WTLS原理 作者:yawl < yawl@nsfocus.com >主页:http://www.nsfocus.com日期:2001-02-19 一 前言 首先要澄清一下 ...

  9. ES6学习笔记(三):与迭代相关的新东东

    Symbol 概念 Symbol:一种新的原始数据类型,表示独一无二的值. 注意:Symbol函数的参数只是表示对当前Symbol值的描述,因此相同参数的Symbol函数的返回值是不相等的. // 没 ...

  10. 【设计模式】—— 策略模式Strategy

    前言:[模式总览]——————————by xingoo 模式意图 定义一系列的算法,把他们封装起来,使得算法独立于适用对象. 比如,一个系统有很多的排序算法,但是使用哪个排序算法是客户对象的自有.因 ...