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 ...
随机推荐
- Vue 编程之路(一)——父子组件之间的数据传递
最近公司的一个项目中使用 Vue 2.0 + element UI 实现一个后台管理系统的前端部分,属于商城类型.其中部分页面是数据管理页,所以有很多可以复用的表格,故引入自定义组件.在这里分享一下开 ...
- python-__getattr__ 和 __getattribute__
python3完全使用了新式类,废弃了旧式类,getattribute作为新式类的一个特性有非常奇妙的作用.查看一些博客和文章后,发现想要彻底理解getattr和getattribute的区别,实际上 ...
- [笔记] postgresql 流复制(streaming replication)
基本环境说明: os:FreeBSD 9.3 postgresql version: master:192.168.56.101 standby:192.168.56.102 安装过程略,基于pkg包 ...
- 第一次c++团队合作项目第三篇随笔
这次终于想出来了上次问题的解决方法,就是用多态的方法,让小兵,建筑和英雄继承于Object类,通过指针能实现信息的传递. 同时我也完善了地图中每个Pane类的信息,包括每个格子的位置信息,state( ...
- 设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接
标题:设计 Azure SQL 数据库,并使用 C# 和 ADO.NET 进行连接 里面有使用C#使用SqlServer的例子.
- 安装DHCP 服务器 指的是由服务器控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码
DHCP服务详解 前言:动态主机配置协议,给局域网内的主机分配IP地址,子网掩码,网关,DNS ARP协议 arp: address resolveing protocol (地址解析协议) 实现:I ...
- CCF——数位之和201512-1
问题描述 给定一个十进制整数n,输出n的各位数字之和. 输入格式 输入一个整数n. 输出格式 输出一个整数,表示答案. 样例输入 20151220 样例输出 13 样例说明 20151220的各位数字 ...
- UML之Enterprise Architect使用
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:UML之Enterprise Architect使用 本文地址:http://tech ...
- win8平板APP开发的教程文章
http://blog.csdn.net/tcjiaan/article/details/7866595 基于C#的Metro工程如何引用C++的动态库——FIleNotFound解决办法: http ...
- PHP面向对象之接口
接口(interface)技术 什么是接口? 先看抽象类: abstract class 类名 { 属性1: 属性2: ..... 非抽象方法1: 非抽象方法2: ...... 抽象方法1: 抽 ...