hdu 2433 Travel(还不会)
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.
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.
5 4
5 1
1 3
3 2
5 4
2 2
1 2
1 2
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(还不会)的更多相关文章
- hdu 2433 Travel
http://acm.hdu.edu.cn/showproblem.php?pid=2433 题意: 求删除任意一条边后,任意两点对的最短路之和 以每个点为根节点求一个最短路树, 只需要记录哪些边在最 ...
- HDU 2433 Travel (最短路,BFS,变形)
题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...
- hdu 2433 Travel (最短路树)
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) ...
- hdu 5380 Travel with candy(双端队列)
pid=5380">题目链接:hdu 5380 Travel with candy 保持油箱一直处于满的状态,维护一个队列,记录当前C的油量中分别能够以多少价格退货,以及能够推货的量. ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- HDU 2433 (最短路+BFS+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=2433 这个问题因为路径都是1,所以可以用bfs遍历 可以看这几篇文章讲解: http://blog.csdn.n ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- hdu 5441 Travel(并查集)
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
随机推荐
- Integer Numbers
ZOJ Problem Set - 3365 Integer Numbers Time Limit: 1 Second Memory Limit: 32768 KB Special ...
- JVM学习十二:JVM之性能监控工具
前面我们学习了很多JVM相关的理论知识,那么本节将重点讲述的是工具的使用,正所谓:工欲善其事,必先利其器.因此,本节介绍常用的性能监控工具,用于性能监控和问题排查. 一.系统性能监控 系统性能工具用于 ...
- JobEngine 基于quartz.net 跨平台作业框架
github:https://github.com/zzhi/JobEngine 基于quartz.net 的跨平台作业框架 quartz.net(https://github.com/quartzn ...
- TED_Topic3:The hidden reason for poverty the world needs to address now
The hidden reason for poverty the world needs to address now By Gary Haugen # Background about our s ...
- Configure Always On Availability Group for SQL Server on Ubuntu——Ubuntu上配置SQL Server Always On Availability Group
下面简单介绍一下如何在Ubuntu上一步一步创建一个SQL Server AG(Always On Availability Group),以及配置过程中遇到的坑的填充方法. 目前在Linux上可以搭 ...
- 吐泡泡(2018年全国多校算法寒假训练营练习比赛(第二场)+栈模拟)+Plug-in(codeforces81A+栈模拟)
吐泡泡题目链接:https://www.nowcoder.com/acm/contest/74/A 题目: 思路: 这种题目当初卡了我很久,今天早训时遇到一个一样得题,一眼就想到用栈模拟,就又回来把这 ...
- NYOJ 231 Apple Tree (树状数组)
题目链接 描述 There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in t ...
- Mac 10.9x下安装配置phonegap3.0开发环境 (涉及android sdk配置)
最近突然想弄一下phonegap,之前一直是听说,没亲自配置开发过.结果配置过程非常艰难啊.特别是android平台的配置,那叫一个麻烦,网上搜了半天都没找到非常好的资料.文章也都是抄来抄去,最烦的就 ...
- layui的模块化和非模块化使用
非模块化和模块化的区别是 非模块化不用每次都调用layui.use([],fun...)引入对应模块,引入的JS是/layui/layui.all.js 模块化必须每次都调用layui.use([], ...
- UBIFS学习笔记
在做项目的时候,发现flash芯片有异常现象,经过打印分析,发现是UBIFS方面设置有一些问题,经过查阅一部分资料,最终得到问题的答案. 在解决问题的过程中,发现打印信息比较重要,但网上并没有直接的相 ...