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. stl源码剖析 详细学习笔记 set map

    // //  set map.cpp //  笔记 // //  Created by fam on 15/3/23. // // //---------------------------15/03 ...

  2. flask-login 整合 pyjwt + json 简易flask框架

    现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...

  3. sonarqube扫描安卓代码

    代码才用https://github.com/liwanlei/bilibili-android-client 配置: build.gralde配置 buildscript { repositorie ...

  4. Docker 部署学习

    https://yeasy.gitbooks.io/docker_practice/basic_concept/repository.html https://hujb2000.gitbooks.io ...

  5. laravel从5.2到5.5从入门到精通视频教程共16套

    laravel从5.2到5.5从入门到精通视频教程共16套,大部分都是实战项目比如P2P.博客.短网址.知乎门户.app软件开发.微信商城实战等 课程目录: 01.Laravel框架从入门到精通02. ...

  6. 12.16daily_scrum

    这个阶段,我们组需要攻克的技术难题一个是测试及美化界面,另一个是在M1阶段的基础上进一步细化和完善悬浮窗的功能,具体的工作内容如下: 具体工作: 小组成员 今日任务 明日任务 工作时间 李睿琦 图片笔 ...

  7. Daily Scrum - 11/17

    今天小组例会内容较少.拜重阳将一个简易的UI设计好push上TFS了,其他人没有太多进展.在原有项目基础上继续开发看似工作量变少,其实开始需要弄清楚原先代码的实现架构和各种借口还是比较困难的.

  8. nodeJs 接收请求参数和发送请求参数

    接收请求: request: (1) req.query (2) 导入中间件:var bodyParser = require('body-parser') req.body 响应: response ...

  9. PAT 甲级 1090 Highest Price in Supply Chain

    https://pintia.cn/problem-sets/994805342720868352/problems/994805376476626944 A supply chain is a ne ...

  10. idea中 mybatis的debug文件需要放在src的目录下 不能加多余的路径