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
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<numeric>//STL数值算法头文件
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<functional>//模板类头文件
using namespace std;
#define N 110
#define M 6020
const int INF=0x3f3f3f3f;
const int maxn=1010; struct Edgs
{
int to,next;
} E[M]; struct Node
{
int x, y;
} node[M]; int d[N], pre[N][N], num[N][N], sum[N], head[N];
int cnt, n, m;
bool flag = true;
bool vis[N]; void add_edgs(int u, int v)
{
E[cnt].to = v;
E[cnt].next = head[u];
head[u] = cnt++;
} void init()
{
memset(num, 0, sizeof(num));
memset(head, -1, sizeof(head));
cnt = 0;
int x, y;
for (int i = 0; i < m; i++)
{
scanf("%d%d", &x, &y);
node[i].x = x;
node[i].y = y;
num[x][y]++;
num[y][x]++;
add_edgs(x, y);
add_edgs(y, x);
}
} void bfs(int s)
{
queue<int> q;
for (int i = 1; i <= n; i++)
{
d[i] = INF;
vis[i] = false;
}
d[s] = 0;
pre[s][s] = 0;
vis[s] = 1;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop(); for (int i = head[u]; i != -1; i = E[i].next)
{
int v = E[i].to;
if (!vis[v])
{
d[v] = d[u] + 1;
vis[v] = 1;
pre[s][v] = u;
q.push(v);
}
}
}
sum[s] = 0;
for (int i = 1; i <= n; i++)
{
if (d[i] == INF)
{
flag = false;
return ;
}
sum[s] += d[i];
}
} int bfs2(int s)
{
queue<int> q;
for (int i = 1; i <= n; i++)
{
vis[i] = false;
d[i] = INF;
}
d[s] = 0;
vis[s] = true;
q.push(s); while (!q.empty())
{
int u = q.front();
q.pop(); for (int i = head[u]; i != -1; i = E[i].next)
{
int v = E[i].to;
if (num[u][v] && !vis[v])
{
d[v] = d[u] + 1;
vis[v] = true;
q.push(v);
}
}
} int ans = 0;
for (int i = 1; i <= n; i++)
{
if (d[i] == INF)
return -1;
ans += d[i];
}
return ans;
} void solve()
{ flag = true;
for (int i = 1; i <= n; i++)
{
if (flag)
bfs(i);
else
break;
} for (int i = 0; i < m; i++)
{ if (!flag)
{
printf("INF\n");
continue;
}
int x = node[i].x;
int y = node[i].y; int ans = 0, j;
for (j = 1; j <= n; j++)
{
if (pre[j][x] != y && pre[j][y] != x)
{
ans += sum[j];
continue;
}
num[x][y]--;
num[y][x]--; int t = bfs2(j); num[y][x]++;
num[x][y]++; if (t == -1)
{
printf("INF\n");
break;
}
ans += t;
}
if (j == n + 1)
printf("%d\n", ans);
}
} int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
init();
solve();
}
return 0;
}

hdu 2433 Travel(还不会)的更多相关文章

  1. hdu 2433 Travel

    http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...

  2. HDU 2433 Travel (最短路,BFS,变形)

    题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...

  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. HDU3068 最长回文 MANACHER+回文串

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3068 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符 ...

  2. 重构改善既有代码设计--重构手法18:Self Encapsulate Field (自封装字段)

    你直接访问一个值域(field),但与值域之间的耦合关系逐渐变得笨拙. 为这个值域建立取值/设值函数(getting/setting methods),并且只以这些函数来访问值域. private i ...

  3. bootstrap下laydate样式错乱问题

    查看发现bs使用了 * {box-sizing:border-box;}重置了盒子模型 那么我们再把它重置回来,在样式中加入以下代码 .laydate_box, .laydate_box * { bo ...

  4. 10款好用的 jQuery 图片切换效果插件

    jQuery 是一个非常优秀的 Javascript 框架,使用简单灵活,同时还有许多成熟的插件可供选择.其中,最令人印象深刻的应用之一就是对图片的处理,它可以让帮助你在你的项目中加入一些让人惊叹的效 ...

  5. javascript在IE下不能用 trim函数解决方法

    javascript 的trim 函数在firefox 下面使用没有问题 <script language="javascript"> var test1 = &quo ...

  6. Stat2—主成分分析(Principal components analysis)

    最近在猛撸<R in nutshell>这本课,统计部分涉及的第一个分析数据的方法便是PCA!因此,今天打算好好梳理一下,涉及主城分析法的理论以及R实现!come on…gogogo… 首 ...

  7. MSSQL 基础知识002

    ---启用sa账号 1. 先使用一个windows账号登陆. 2.在数据库实例上面右键,属性,安全性,登录名,sa. 右键,属性. 常规,修改sa的密码. 状态,启用sa账号. 主键的作用: 1.唯一 ...

  8. (转)USB体系结构

    转载地址:http://blog.ednchina.com/zenhuateng/203584/Message.aspx USB总线接口层:物理连接.电气信号环境.信息包传输机制:主机一方由USB主控 ...

  9. 64_t4

    texlive-hardwrap-svn21396.0.2-33.fc26.2.noarch.rpm 24-May-2017 15:41 35930 texlive-harmony-doc-svn15 ...

  10. Oracle-AWR报告简介及如何生成【转】

    AWR报告 awr报告是oracle 10g及以上版本提供的一种性能收集和分析工具,它能提供一个时间段内整个系统资源使用情况的报告,通过这个报告,我们就可以了解Oracle数据库的整个运行情况,比如硬 ...