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. 强制换行CSS样式

    语法: word-wrap : normal | break-word 取值: normal :? 默认值.允许内容顶开指定的容器边界 break-word :? 内容将在边界内换行.如果需要,词内换 ...

  2. 使用JSON Web Token设计单点登录系统

    用户认证八步走 所谓用户认证(Authentication),就是让用户登录,并且在接下来的一段时间内让用户访问网站时可以使用其账户,而不需要再次登录的机制. 小知识:可别把用户认证和用户授权(Aut ...

  3. Linux必备工具Tmux

    之前介绍了Linux的Screen命令,今天介绍一个更为强大的终端工具Tmux. Tmux 是一个用于在一个终端窗口中运行多个终端会话的工具.它基本能替代nohup以及screen,甚至比它们更为强大 ...

  4. 解决Chrome下表单自动填充后背景色为黄色

    Chrome浏览器在表单自动填充后会显示黄色背景,这是Chrome的私有属性导致,对于有洁癖的人来讲,是不喜欢的,我们可以手动去掉. 代码如下: input:-webkit-autofill { -w ...

  5. 蓝色简单的cms文档管理系统模板——后台

    链接:http://pan.baidu.com/s/1qYMwHis 密码:xyiw

  6. Feather包实现数据框快速读写,你值得拥有

    什么是Feather? Feature是一种文件格式,支持R语言和Python的交互式存储,速度更快.目前支持R语言的data.frame和Python pandas 的DataFrame. Feat ...

  7. Coursera在线学习---第二节.Octave学习

    1)两个矩阵相乘 A*B 2)两个矩阵元素位相乘(A.B矩阵中对应位置的元素相乘) A.*B 3)矩阵A的元素进行平方 A.^2 4)向量或矩阵中的元素求倒数 1./V    或   1./A 5) ...

  8. C语言回调函数总结

    /* Main program ---calls--> Library function ---calls--> Callback funtion */ #include <stdi ...

  9. ubuntu12.04 svn ssl错误

    1,ubuntu12.04 svn ssl错误提示: OPTIONS of '<url>': SSL handshake failed: SSL error: Key usage viol ...

  10. 45.Jump Game II---贪心---2018大疆笔试题

    题目链接 题目大意:与55题类似,只是这里要求出跳数. 法一(借鉴):贪心.cur表示当前能到达的最远距离,pre表示上一次能到达的最远距离,每到一个位置更新一次最远距离cur,如果当前位置超过了上一 ...