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. PHP扩展--opcache安装及配置

    简介 Optimizer+ 是 Zend 开发的闭源但可以免费使用的 PHP 优化加速组件,是第一个也是最快的 opcode 缓存工具.现在,Zend 科技公司将 Optimizer+ 在 PHP L ...

  2. MappedByteBuffer以及ByteBufer的底层原理

    最近在用java中的ByteBuffer,一直不明所以,尤其是对MappedByteBuffer使用的内存映射这个概念云里雾里. 于是首先补了物理内存.虚拟内存.页面文件.交换区的只是:小科普——物理 ...

  3. 如何阻止自动更新‘updated_at’和'created_at'

    可以在模版中添加一条代码: public $timestamps = false;

  4. NYOJ 141 Squares (数学)

    题目链接 描述 A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degre ...

  5. 2018 黑盾杯部分writeup

    这次比赛拿了三十多名.呵呵.或许这就是菜吧. 比赛经验教训: 1.赛前一定要调整好情绪. 2.比赛尽量不要紧张,每一题都认认真真的看过去! 3.站在出题者的角度去思考问题 4.刷题 5.还是情绪吧.其 ...

  6. MYSQL的隐式类型转换

    官方文档中是这么说的 当操作者使用不同类型的操作数,操作数类型兼容的出现使 转换.一些 发生隐式转换.例如,MySQL会自动 将数字转换为字符串的必要,反之亦然. 也可以将数字转换为字符串明确 使用( ...

  7. 64_p1

    PEGTL-devel-1.3.1-2.fc26.i686.rpm 13-Feb-2017 22:10 64086 PEGTL-devel-1.3.1-2.fc26.x86_64.rpm 13-Feb ...

  8. 62.Unique Paths---dp

    题目链接 题目大意:给一个m*n的方格,从左上角走到右下角,中间无任何障碍,问有多少种走法. 法一:DFS,超时,简单模板深搜,无任何剪枝,结果一半的数据超时.代码如下: public int uni ...

  9. Python 库汇总中文版

    这又是一个 Awesome XXX 系列的资源整理,由 vinta 发起和维护.内容包括:Web框架.网络爬虫.网络内容提取.模板引擎.数据库.数据可视化.图片处理.文本处理.自然语言处理.机器学习. ...

  10. C/C++——C语言常用库函数

    本文转载自:https://blog.csdn.net/qq_36955347/article/details/71511900 一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #inc ...