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 ...
随机推荐
- 关于kv的jch分片存储
确定节点同步一致 节点启动之后,先获取本地的addrbook里面的节点信息 根据获取的addrbook里面的节点信息进行校验(向addrbook里面的节点发送hash消息确认,如果都一样,则可以广播数 ...
- pyextend库-unpack列表集合字符串解包函数
pyextend - python extend lib unpack (iterable, count, fill=None) 参数: iterable: 实现 __iter__的可迭代对象, 如 ...
- loadrunner socket协议问题归纳(4)---buffer接收变长和定长的数据
测试场景:聊天系统 用户登录后,要先向服务器发送用户名,然后可以发送聊天信息,同时也可以接受聊天信息. 如果接受的字符为定长时,可以设定接受长度.recv buf2 66 #include " ...
- DCOM初步窥探二
1.COM进程透明性表现在“组件对象和客户程序可以拥有各自的空间,也可以共享同一个进程空间”. COM负责把客户的调用正确传到组件对象中,并保证参数传递的正确性. 组件对象和客户代码不必考虑调用传递的 ...
- python 中如何计算时间差...
Q:如何方便的计算两个时间的差,如两个时间相差几天,几小时等 A:使用datetime模块可以很方便的解决这个问题,举例如下: >>> import datetime>> ...
- FPGA论文
基于 NetFPGA 的 VCP 网络的设计与实现 --可变结构拥塞控制协议(VCP),适应于高带宽时延乘积网络的显式拥塞控制协议 无源光网络(PON) 1.区块链技术发展,物联网设备激增,服务器压力 ...
- 第八,九周web制作代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...
- div跟随鼠标移动
1.目标是实现div跟随鼠标而移动,分三种情况进行实现 a)首先获取div,进行绑定鼠标移动事件,给div开启定位功能 第一种实现方式,假如body的大小跟页面大小一样,则可以用这个方法. 1)获取鼠 ...
- windows平台下nginx+PHP环境安装
因为日常工作在windows下,为方便在window是下进行PHP开发,需要在windows平台下搭建PHP开发环境,web服务器选择nginx,不过windows版本的nginx性能要比Linux/ ...
- 设计模式php篇(一)————单例模式
话不多说,直接上代码: <?php namespace DesignPattern; /** * php设计模式之单例模式 */ class SingleInstance { private s ...