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

Travel

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1572    Accepted Submission(s): 526

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
 

题意:

因为每次只删除一条边,所以删除边后的sum,其实就与这条边上有关,或者所是和这条边上的两个端点有关,
首先 每次删除边都计算一次的暴力做法是不可能ac的, 除了用什么优化,但是也是很慢的。
所以只能在每次删除边的时候,做一些改变。
具体的做法是:
对每一个点求一次最短路,并将其求和,保存在一个数组里头,定为sum[i],i表示着一个点到所有其他点最短路之和。并将这些和相加
ans = sum[1]  + …… + sum[n]; 

      然后,删除一条边,其顶点暂定为u,v,对这条边的一个顶点u在一次求最短路,如果这个点,不能到达这条边的另一个点v,则
直接输出INF

      如果,能够到达,则对v也求一次最短路,对于u,v两点来说,求得u到每一个点的最短路之和sum_u,求得v到每一个点的最短路之和sum_v,

      最后结果为: ans = ans + sum_u + sum_v - sum[u] - sum[v];
程序:
#include"stdio.h"
#include"string.h"
#include"queue"
#include"iostream"
#define inf 10000
#define M 111
using namespace std;
int n,dis[M],use[M];
struct st
{
int u,v,w,next;
}edge[M*M];
int head[M],t;
void init()
{
t=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
edge[t].u=u;
edge[t].v=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
void spfa(int s)
{
int i;
queue<int>q;
memset(use,0,sizeof(use));
for(i=1;i<=n;i++)
dis[i]=inf;
dis[s]=0;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
use[u]=0;
for(i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]>dis[u]+edge[i].w)
{
dis[v]=dis[u]+edge[i].w;
if(!use[v])
{
use[v]=1;
q.push(v);
}
}
}
}
}
int x[3009],y[3009],sum[M];
int main()
{
int m,i,j,sum_v,sum_u;
while(scanf("%d%d",&n,&m)!=-1)
{
init();
for(i=1;i<=m;i++)
{
scanf("%d%d",&x[i],&y[i]);
add(x[i],y[i],1);
add(y[i],x[i],1);
}
int ans=0;
for(i=1;i<=n;i++)
{
sum[i]=0;
spfa(i);
for(j=1;j<=n;j++)
sum[i]+=dis[j];
ans+=sum[i];
}
for(i=1;i<=m;i++)
{
edge[i*2-1].w=edge[i*2-2].w=inf;
spfa(x[i]);
sum_u=0;
for(j=1;j<=n;j++)
sum_u+=dis[j];
if(sum_u>=inf)
{
edge[i*2-1].w=edge[i*2-2].w=1;
printf("INF\n");
continue;
}
sum_v=0;
spfa(y[i]);
for(j=1;j<=n;j++)
sum_v+=dis[j];
printf("%d\n",ans+sum_u+sum_v-sum[x[i]]-sum[y[i]]);
edge[i*2-1].w=edge[i*2-2].w=1;
}
}
}

单源最短路(spfa),删边求和的更多相关文章

  1. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  2. 图论-单源最短路-SPFA算法

    有关概念: 最短路问题:若在图中的每一条边都有对应的权值,求从一点到另一点之间权值和最小的路径 SPFA算法的功能是求固定起点到图中其余各点的的最短路(单源最短路径) 约定:图中不存在负权环,用邻接表 ...

  3. 单源最短路——SPFA算法(Bellman-Ford算法队列优化)

    spfa的算法思想(动态逼近法):     设立一个先进先出的队列q用来保存待优化的结点,优化时每次取出队首结点u,并且用u点当前的最短路径估计值对离开u点所指向的结点v进行松弛操作,如果v点的最短路 ...

  4. HDU 2066 一个人的旅行(单源最短路SPFA)

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  5. 单源最短路SPFA算法

    $huaji^{233……}$模板:洛谷 P3371 #include<iostream> #include<algorithm> #include<cstdio> ...

  6. [模板][HDU]P2544[单源最短路][SPFA]

    题目就不放了,主要是写一下SPFA,很少写,今天特别学了一个用STL的队列来做的. 代码: #include<iostream> #include<cstdio> #inclu ...

  7. 单源最短路SPFA

    #include<iostream> #include<queue> #include<cstring> #define INF 0x3f3f3f3f using ...

  8. 用scheme语言实现SPFA算法(单源最短路)

    最近自己陷入了很长时间的学习和思考之中,突然发现好久没有更新博文了,于是便想更新一篇. 这篇文章是我之前程序设计语言课作业中一段代码,用scheme语言实现单源最段路算法.当时的我,花了一整天时间,学 ...

  9. 模板C++ 03图论算法 1最短路之单源最短路(SPFA)

    3.1最短路之单源最短路(SPFA) 松弛:常听人说松弛,一直不懂,后来明白其实就是更新某点到源点最短距离. 邻接表:表示与一个点联通的所有路. 如果从一个点沿着某条路径出发,又回到了自己,而且所经过 ...

  10. 2018/1/28 每日一学 单源最短路的SPFA算法以及其他三大最短路算法比较总结

    刚刚AC的pj普及组第四题就是一种单源最短路. 我们知道当一个图存在负权边时像Dijkstra等算法便无法实现: 而Bellman-Ford算法的复杂度又过高O(V*E),SPFA算法便派上用场了. ...

随机推荐

  1. jQuery Form插件详解

    <script src="js/jquery.form.js" type="text/javascript"></script> Jqu ...

  2. 非常强大的jQuery万能浮动框插件

    支持hover, click, focus以及无事件触发:支持多达12种位置的定位,出界自动调整:支持页面元素加载,Ajax加载,下拉列表,提示层效果,tip类效果等:可自定义装载容器:内置UI不错的 ...

  3. Ubuntu之Docker安装

    1.添加官方的GPG key到系统 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - 2.添加D ...

  4. Spring Annotation是怎么工作的?

    最近刚好看了下注解,虽然明白了注解的作用原理,但是仍然不明白Spring中的注解是如何工作的. 占座用,留待后续. 先来两个链接吧 https://dzone.com/articles/spring- ...

  5. SSH远程登录其他机器

    常用格式:ssh [-l login_name] [-p port] [user@]hostname更详细的可以用ssh -h查看. 不指定用户: ssh 192.168.0.11 指定用户: ssh ...

  6. unity3d绘画手册-------地形高度调节

    高度 所有地形 (terrain) 编辑工具的使用都很简单.您可以在场景视图 (scene view)中逐步绘制地形 (terrain).对于高度工具和其他所有工具,您只需选中工具,然后在场景视图 ( ...

  7. tiny210V2开发板hdmi输出到10.1寸LCD,无图像

    tiny210V2开发板hdmi输出到10.1寸LCD,无图像... 用tiny210V2开发板的HDMI接口输出到的10.1寸LCD,LCD无任何现象.说明一下我的情况,我的10.1寸屏LCD是HD ...

  8. nodejs基础 -- 模块系统

    为了让nodejs的文件可以相互调用,nodejs提供了一个简单的模块系统. 模块:是nodejs应用程序的基本组成部分,文件和模块一一对应.即,一个nodejs文件就是一个模块,这个文件可能是jav ...

  9. seqtk 的安装和使用

    seqtk 是一款针对fasta/fastq 文件进行处理的小程序,有很多的功能,速度很快,很方便: 源代码:https://github.com/lh3/seqtk 安装: git clone ht ...

  10. CentOS显示设置时间命令- date

    概要: date命令的功能是显示和设置系统日期和时间 命令格式: date [OPTION]... [+FORMAT]date [-u|--utc|--universal] [MMDDhhmm[[CC ...