Hdu2433 Travel
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3391 Accepted Submission(s): 1162
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 1
1 3
3 2
5 4
2 2
1 2
1 2
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ,inf = 0x7ffffff; int n,m,head[],to[maxn * ],nextt[maxn * ],tot = ,pre[][],num[][];
int d[],vis[],sum[];
bool flag = true; struct node
{
int x,y;
} e[maxn]; void add(int x,int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void bfs(int s)
{
queue <int> q;
q.push(s);
for (int i = ; i <= n; i++)
d[i] = inf;
memset(vis,,sizeof(vis));
vis[s] = ;
d[s] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!vis[v])
{
pre[s][v] = u;
d[v] = d[u] + ;
vis[v] = ;
q.push(v);
}
}
}
for (int i = ; i <= n; i++)
{
if(d[i] == inf)
{
flag = false;
return;
}
else
sum[s] += d[i];
}
} int bfs2(int s)
{
queue <int> q;
q.push(s);
for (int i = ; i <= n; i++)
d[i] = inf;
memset(vis,,sizeof(vis));
vis[s] = ;
d[s] = ;
while (!q.empty())
{
int u = q.front();
q.pop();
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!vis[v] && num[u][v])
{
d[v] = d[u] + ;
vis[v] = ;
q.push(v);
}
}
}
int res = ;
for (int i = ; i <= n; i++)
{
if (d[i] == inf)
return -;
else
res += d[i];
}
return res;
} int main()
{
while (scanf("%d%d",&n,&m) != EOF)
{
memset(head,,sizeof(head));
tot = ;
flag = true;
memset(pre,,sizeof(pre));
memset(sum,,sizeof(sum));
memset(num,,sizeof(num));
for (int i = ; i <= m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
num[x][y]++;
num[y][x]++;
e[i].x = x;
e[i].y = y;
add(x,y);
add(y,x);
}
for (int i = ; i <= n; i++)
{
bfs(i);
if(!flag)
break;
}
if (!flag)
{
for (int i = ; i <= m; i++)
puts("INF");
}
else
{
for (int i = ; i <= m; i++)
{
bool flag2 = true;
int ans = ,x = e[i].x,y = e[i].y;
for (int j = ; j <= n; j++)
{
if (pre[j][y] != x && pre[j][x] != y)
{
ans += sum[j];
continue;
}
else
{
num[x][y]--;
num[y][x]--;
int t = bfs2(j);
num[x][y]++;
num[y][x]++;
if (t == -)
{
flag2 = false;
puts("INF");
break;
}
else
ans += t;
}
}
if (flag2)
printf("%d\n",ans);
}
}
} return ;
}
Hdu2433 Travel的更多相关文章
- HDU2433—Travel (BFS,最短路)
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- HDU2433 BFS最短路
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- 【BZOJ-1576】安全路径Travel Dijkstra + 并查集
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1044 Solved: 363[Sub ...
- Linux inode && Fast Directory Travel Method(undone)
目录 . Linux inode简介 . Fast Directory Travel Method 1. Linux inode简介 0x1: 磁盘分割原理 字节 -> 扇区(sector)(每 ...
- HDU - Travel
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- ural 1286. Starship Travel
1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equ ...
- Travel Problem[SZU_K28]
DescriptionAfter SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingC ...
随机推荐
- iOS 播放音频文件
// 播放音乐 NSString *path = [[NSBundle mainBundle] pathForResource:@"1670" ofType:@&qu ...
- ZOJ 3962
就是统计1~n中出现的各个数字的次数,当然是在16进制下. 不过有个区间问题的小技巧,统计从 [x,y] 可以转换成 从 [1,y] 减去 [1,x-1]. 不过要分类讨论一下,因为有可能会出现溢出, ...
- Adobe InDesign CS6自学入门到高级视频教程
关键字:Adobe InDesign 视频教程 点击获取视频教程 教程目录 第1章/1.卸载InDesign CS6.avi 第1章/2.安装InDesign CS6.avi 第2章/1.创建并编辑自 ...
- sip鉴权认证算法详解及python加密
1. 认证和加密 认证(Authorization)的作用在于表明自己是谁,即向别人证明自己是谁.而相关的概念是MD5,用于认证安全.注意MD5仅仅是个hash函数而已,并不是用于加密.因为ha ...
- Planning The Expedition(暴力枚举+map迭代器)
Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...
- AVL树 算法思想与代码实现
AVL树是高度平衡的二叉搜索树,按照二叉搜索树(Binary Search Tree)的性质,AVL首先要满足: 若它的左子树不为空,则左子树上所有结点的值均小于它的根结点的值: 若它的右子树不为空, ...
- 1029对c语言文法的理解
<程序>→<外部声明>|<程序><外部声明> <外部声明>→<函数定义>|<声明> <函数定义>→< ...
- 3.结对编程成果报告(小学生四则运算的出题程序,Java实现)
程序名称:小学生四则运算的出题程序 先附上代码: package com.makequestion; import java.text.DecimalFormat;import java.util.R ...
- Kotlint集合简单总结
1.数组操作 var testArray = Array<>("s","ss")或者 = arrayOf("s","s ...
- Django内置的分页模块
自定义分页 未封装版: 优点:直观 缺点:代码乱,不易维护,可拓展性差 data = [] for i in range(1, 302): tmp = {"id": i, &quo ...