hdu2433 bfs+优化
来源:互联网 发布:矩阵计算 袁亚湘 编辑:程序博客网 时间:2024/06/03 09:03
Travel
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1120 Accepted Submission(s): 381
Problem Description
One day, Tom traveled to a country named BGM. BGM is a small country, but there are N (N <= 100) towns in it. Each town products one kind of food, the food will be transported to all the towns. In addition, the trucks will always take the shortest way. 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.
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.
Input
The input contains several test cases.
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.
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.
Output
Output M lines, the i-th line is the new SUM after the i-th road is destroyed. If the towns are not connected after the i-th road is destroyed, please output “INF” in the i-th line.
Sample Input
5 45 11 33 25 42 21 21 2
Sample Output
INFINFINFINF22
Source
2008 Asia Chengdu Regional Contest Online
Recommend
lcy
思路来自:http://hi.baidu.com/novosbirsk/item/bfcf0cd201edfc2d39f6f709
暴力算法:枚举每条边,然后对每个点求一次单源最短路,由于题目中的边的权值都是1,因此可以用bfs来求得每个点的单源最短路。复杂度是O(M*N*M)。由于M*N*M=3000*100*3000=9*108,不可能AC。
标程算法:仍然使用上面的思路,但要作一些预处理。对每个顶点u求一次单源最短路,把求得的结果称作u的最短路径树,并用数组记录所有点到 其他所有顶点的路径的和。若被破坏的公路不在该最短路径树上,则从u出发的所有最短路径的总和就是u到该树上的所有顶点的路径的总和,因为刚刚记录了这个 数值,因此花费O(1)时间就能返回结果。否则,删除被破坏的公路后,重新通过BFS计算从u出发的所有最短路径的总和,
#include <iostream>#include <cstring>#include <cstdio>#include <queue>#define inf 99999999using namespace std;int map[200][200];int sum[200];struct node{ int u,v;};node edge[3005];int pre[200][200];int first;int bfs(int u,int n){ int vis[200]; int dd[200]; memset(vis,0,sizeof(vis)); memset(dd,0,sizeof(dd)); vis[u]=1; dd[u]=0; int i; queue<int> q; q.push(u); int count=1; int s=0; while(!q.empty()) { int t=q.front(); q.pop(); for(i=1;i<=n;i++) { if(!vis[i]&&map[t][i]) { dd[i]=dd[t]+1; if(first) //只在第一次做, pre[u][i]=t; //关键优化, vis[i]=1; s+=dd[i]; q.push(i); count++; } } } if(count==n) { return s; } else return inf;}int main(){ int n,m; while(scanf("%d%d",&n,&m)!=EOF) { memset(map,0,sizeof(map)); memset(sum,0,sizeof(sum)); int i; memset(pre,0,sizeof(pre)); first=1; for(i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); map[a][b]++; map[b][a]++; edge[i].u=a; edge[i].v=b; } int flag=0; int ss=0; for(i=1;i<=n;i++) { sum[i]=bfs(i,n); ss+=sum[i]; if(sum[i]==inf) { flag=1; break; } } if(flag) { for(i=0;i<m;i++) printf("INF\n"); continue; } first=0; for(i=0;i<m;i++) { int s=0; if(map[edge[i].u][edge[i].v]>1) printf("%d\n",ss); else if(map[edge[i].u][edge[i].v]==1) { int j; for(j=1;j<=n;j++) { if(pre[j][edge[i].v]!=edge[i].u&&pre[j][edge[i].u]!=edge[i].v) //关键优化 迅速判断出该边是不是最短树上的路径 s+=sum[j]; else { map[edge[i].u][edge[i].v]--; map[edge[i].v][edge[i].u]--; s+=bfs(j,n); if(s>=inf) break; map[edge[i].u][edge[i].v]++; map[edge[i].v][edge[i].u]++; } } if(j>n) printf("%d\n",s); else printf("INF\n"); } } } return 0;}
- hdu2433 bfs+优化
- HDU2433 Travel BFS求最短路径树+优化
- hdu2433 BFS最短路
- HDU2433-预处理优化
- hdu2433
- [HDU2433] Travel BFS+最短路生成树
- hdu2433 spfa+mark[x][u][v]优化
- hdu2433 Travel
- HDU2433(SPFA)
- hdu2433之最短路
- hdu2433 最短路+思维
- 双向BFS及优化
- BFS+堆优化
- Travel (BFS + 预处理优化)
- HDU2612 BFS优化
- hdu2433(最短路径树)
- UVA11624:Fire!(BFS + 优化)
- Prime Path (bfs+优化)
- 今天我生日
- 删除单向链表中的某一个节点
- 第五堂课后作业
- Java对多线程的支持(一) - Thread类、Runnable接口、Daemon线程、线程同步
- 1.2.4---Palindromic Squares
- hdu2433 bfs+优化
- 1207: Strictly-increasing sequence
- Twister编译及安装 [多节点方式]
- Hibernate注解配置 在Spring的配置文件里写法
- Java Ant build.xml详解
- 一推荐的Git 分支模型
- poj - 2833- The Average
- java 如何使的float保留2位或者多位小数
- 异常学习笔记