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,对于每一个询问,计算出有多少点 ...
随机推荐
- LightOJ 1096 - nth Term 矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1096 题意:\(f(n) = a * f(n-1) + b * f(n-3) + c, ...
- c# windows server安装启动与卸载
使用installutil.exe安装卸载服务时,由于需要指向服务的全路径,由于生成目录往往不是服务发布的最终目录,很不便利,下面介绍两种方式方便操作: 方式一: 项目中加入install.bat与u ...
- GridControl详解(五)设置行备注和行号
备注显示设置 设置备注字段 显示结果: 可以写入按键事件F3,用以开关备注显示 private void Form4_KeyUp(object sender, KeyEventArgs e) { if ...
- 在JavaScript中重写jQuery对象的方法
jQuery是一个很好的类库,它给我们解决了很多的客户端编程,任何东西都不是万能的,当它不能满足我们的需求时我们需要对它进行重写,同时也不要影响其原有的功能或者修改其原有的功能:我现在的web应用程序 ...
- 【BZOJ】3302: [Shoi2005]树的双中心 && 2103: Fire 消防站 && 2447: 消防站
[题意]给定带点权树,要求选择两个点x,y,满足所有点到这两个点中较近者的距离*点权的和最小.n<=50000,h<=100. [算法]树的重心 [题解]代码参考自:cgh_Andy 观察 ...
- 【洛谷 P2147】 [SDOI2008]洞穴勘测(LCT)
题目链接 LCT裸题.. #include <cstdio> #define R register int #define I inline void #define lc c[x][0] ...
- Linux 脚本内容指定用户执行
#!/bin/bash set -x ## 因为这些变量在下面要用,所以要写在最上面, ## 如果直接写在下面,则变量获取不到,并且下面的 $ 标识的都要用 引号引起来,否则这些参数接收不到 tarf ...
- 选择问题(选择数组中第K小的数)
由排序问题可以引申出选择问题,选择问题就是选择并返回数组中第k小的数,如果把数组全部排好序,在返回第k小的数,也能正确返回,但是这无疑做了很多无用功,由上篇博客中提到的快速排序,稍稍修改下就可以以较小 ...
- python并发编程之Queue线程、进程、协程通信(五)
单线程.多线程之间.进程之间.协程之间很多时候需要协同完成工作,这个时候它们需要进行通讯.或者说为了解耦,普遍采用Queue,生产消费模式. 系列文章 python并发编程之threading线程(一 ...
- LAMP结合discuz论坛的配置
一.安装discuz ---->//download discuz; [root@localhost ~]# mkdir /data/www [root@localhost ~]# cd /da ...