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. Linux之zip压缩

    1.压缩 对于test目录,使用 zip -rq test.zip test r表示递归压缩,q表示不显示过程 2.解压缩 unzip -q test.zip

  2. TypeError: datetime.datetime(2016, 9, 25, 21, 12, 19, 135649) is not JSON serializable解决办法(json无法序列化对象的解决办法)

    1.一个简单的方法来修补json模块,这样序列将支持日期时间. import json import datetime json.JSONEncoder.default = lambda self, ...

  3. css -- 背景图片自适应屏幕大小

    由于<body>标签的图片不能够拉伸, 解决办法: 1.图片不够大,又background属性不能拉伸图片: 2.只能用个div,把其z-index值设为负,并使这个div大小为整个bod ...

  4. CentOS查看你是否有USB 3.0端口

    近来的大多数的新计算机都有了USB 3.0接口了.但是你怎么知道你的计算机有没有USB 3.0接口?这篇短文中,我们会告诉如何在Linux下知道你的系统上有USB 3还是USB3接口. 在Linux终 ...

  5. CentOS安装emacs24.2命令

    CentOS安装emacs24.2命令 #1.安装如下软件 yum -y groupinstall "Development Tools" yum -y install gtk+- ...

  6. ubuntu设置中文拼音输入法

    转载  http://www.cnblogs.com/zhj5chengfeng/archive/2013/06/23/3150620.html

  7. win8 学习笔记二 输出日志

    Win8 Store App的日志输出不像Desktop App 那么简单,见 这篇文档(http://www.cnblogs.com/xiaokang088/archive/2011/12/27/2 ...

  8. 阿里巴巴CI/CD之分层自动化

    一佛是阿里巴巴B2B事业群高级产品经理.从事多年互联网系统的研发和测试工作,目前主要负责云效分层自动化测试的产品设计.因为自动化测试在实践过程中,总是碰到各种各样的问题,导致进入自动化测试盲区.所以, ...

  9. Apache Commons工具集简介

  10. Android 4.0 Tabhost图标显示不出来

    安卓4.0会有这个问题,修改Manifest.xml里面的Theme,找到System Resources,里面有Theme.black,选这个就行了.剩下自己要改背景色什么的这个还是比较easy的吧 ...